Formatting and Parsing a Time for a Locale Using Default Formats - Java Date Time

Java examples for Date Time:Legacy Date Format

Description

Formatting and Parsing a Time for a Locale Using Default Formats

Demo Code

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

public class Main {

  public void main(String[] argv) {
    Locale locale = Locale.ITALIAN;
    Date date = new Date();

    String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale)
        .format(date);//from   w w w  .  ja  v a 2  s. c om
    System.out.println(s);

    s = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale).format(date);
    System.out.println(s);

    s = DateFormat.getTimeInstance(DateFormat.LONG, locale).format(date);
    System.out.println(s);

    s = DateFormat.getTimeInstance(DateFormat.FULL, locale).format(date);
    System.out.println(s);

    s = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(date);
    System.out.println(s);

    try {
      date = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).parse("22.33.03");
    } catch (ParseException e) {
    }
  }
}

Related Tutorials