The Java Developers Almanac 1.4


Order this book from Amazon.

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

e323. Formatting and Parsing a Date 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("E, dd MMM yyyy", locale);
    String s = formatter.format(new Date());
    // mar., 29 janv. 2002
    
    // Format with a default format
    s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
    // 29 janv. 2002
    
    
    try {
        // Parse with a custom format
        formatter = new SimpleDateFormat("E, dd MMM yyyy", locale);
        Date date = (Date)formatter.parse("mar., 29 janv. 2002");
    
        // Parse with a default format
        date = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).parse("29 janv. 2002");
    } catch (ParseException e) {
    }

 Related Examples
e320. Formatting a Date Using a Custom Format
e321. Parsing a Date Using a Custom Format
e322. Formatting and Parsing a Date Using Default Formats

See also: Messsages    Numbers    Times    Words and Sentences   


© 2002 Addison-Wesley.