Java DateFormat format time with all styles

Description

Java DateFormat format time with all styles

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

public class Main {
  public static void main(String[] args) throws Exception {
    Locale currentLocale = new Locale("fr", "FR");

    Date today = new Date();
    String result;//from   w ww. ja  va 2  s.c  om
    DateFormat formatter;

    int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL };

    System.out.println();
    System.out.println("Locale: " + currentLocale.toString());
    System.out.println();

    for (int k = 0; k < styles.length; k++) {
      formatter = DateFormat.getTimeInstance(styles[k], currentLocale);
      result = formatter.format(today);
      System.out.println(result);
    }
  }
}

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  .  j a  va  2  s . c o m
    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) {
    }
  }
}



PreviousNext

Related