e318. formatting and parsing a time for a locale using default formats.txt

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

TXT
30
字号
Every locale has four default formats for formatting and parsing times. They are called SHORT, MEDIUM, LONG, and FULL. The SHORT format consists entirely of numbers while the FULL format contains most of the time components. There is also a default format called DEFAULT and is the same as MEDIUM. 
Note: This example formats dates using the default locale (which, in the author's case, is Locale.ENGLISH). If the example is run in a different locale, the text (e.g., month names) will not be the same. 

    // Format
    Locale locale = Locale.ITALIAN;
    Date date = new Date();
    
    String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
    // 22.33
    
    s = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale).format(date);
    // 22.33.03
    
    s = DateFormat.getTimeInstance(DateFormat.LONG, locale).format(date);
    // 22.33.03 PST
    
    s = DateFormat.getTimeInstance(DateFormat.FULL, locale).format(date);
    // 22.33.03 PST
    
    s = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(date);
    // 22.33.03
    
    // Parse
    try {
        date = DateFormat.getTimeInstance(
            DateFormat.DEFAULT, locale).parse("22.33.03");
    } catch (ParseException e) {
    }

⌨️ 快捷键说明

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