e319. formatting and parsing a time for a locale.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 24 行

TXT
24
字号
To format and parse in a particular locale, specify the locale when creating the SimpleDateFormat object. 
    Locale locale = Locale.FRENCH;
    
    // Format with a custom format
    DateFormat formatter = new SimpleDateFormat("HH:mm:ss zzzz", locale);
    String s = formatter.format(new Date());
    // 21:44:07 Heure normale du Pacifique
    
    // Format with a default format
    s = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale).format(new Date());
    // 21:44:07
    
    
    try {
        // Parse with a custom format
        formatter = new SimpleDateFormat("HH:mm:ss Z", locale);
        Date date = (Date)formatter.parse("21:44:07 Heure normale du Pacifique");
    
        // Parse with a default format
        date = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale).parse("21:44:07");
    } catch (ParseException e) {
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?