The Java Developers Almanac 1.4


Order this book from Amazon.

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

e319. Formatting and Parsing a Time for a Locale

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

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

See also: Dates    Messsages    Numbers    Words and Sentences   


© 2002 Addison-Wesley.