Formatting the Time using a Locale



Introduction

In a separate article we already formatted Date  and Time using DateFormat. This shows an example of formatting in any Locale.

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.