Java Date time parse and format with Locale

Description

Java Date time parse and format with Locale

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
  public static void main(String[] argv) {
    Locale locale = Locale.FRENCH;

    // Format with a custom format
    DateFormat formatter = new SimpleDateFormat("HH:mm:ss zzzz", locale);
    String s = formatter.format(new Date());
    System.out.println(s);//  w ww .  j  a  v  a 2s .c o m
    // Format with a default format
    s = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale).format(new Date());
    System.out.println(s);
    try {
      formatter = new SimpleDateFormat("HH:mm:ss Z", locale);
      Date date = (Date) formatter.parse("21:44:07 Heure normale du Pacifique");
      System.out.println(date);
      // Parse with a default format
      date = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale).parse("21:44:07");
      System.out.println(date);
    } catch (ParseException e) {
    }
  }
}



PreviousNext

Related