The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.text  [26 examples] > Times  [4 examples]

e318. Formatting and Parsing a Time for a Locale Using Default Formats

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) {
    }

 Related Examples
e316. Formatting the Time Using a Custom Format
e317. Parsing the Time Using a Custom Format
e319. Formatting and Parsing a Time for a Locale

See also: Dates    Messsages    Numbers    Words and Sentences   


© 2002 Addison-Wesley.