Formatting and Parsing a Time for a Locale - Java Date Time

Java examples for Date Time:Legacy Date Parse

Description

Formatting and Parsing a Time for a Locale

Demo Code

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());

    // Format with a default format
    s = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale)
        .format(new Date());

    try {//from   www.j  a va 2  s .co m
      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 Tutorials