Java DateFormat format Date with all styles

Description

Java DateFormat format Date 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;//ww w.  j  a  v a2  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
                .getDateInstance(styles[k], currentLocale);
        result = formatter.format(today);
        System.out.println(result);
    }
  }
}
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class Main {
  public static void main(String[] argv) throws Exception {
    // Format//from  ww  w.j a v  a 2 s. c o m
    Date date = new Date();

    String s = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
    // 2/16/02

    s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(date);
    // Feb 16, 2002

    s = DateFormat.getDateInstance(DateFormat.LONG).format(date);
    // February 16, 2002

    s = DateFormat.getDateInstance(DateFormat.FULL).format(date);
    // Saturday, February 16, 2002

    // This is same as MEDIUM
    s = DateFormat.getDateInstance().format(date);
    // Feb 16, 2002

    // This is same as MEDIUM
    s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(date);
    // Feb 16, 2002

    // Parse
    try {
      date = DateFormat.getDateInstance(DateFormat.DEFAULT).parse(
          "Feb 16, 2017");
    } catch (ParseException e) {
    }
  }
}



PreviousNext

Related