Java DateFormat predefined styles

Introduction

Java DateFormat class has predefined format styles.

The DateFormat class defines five styles as constants:

  • DateFormat.DEFAULT Mar 27, 2018
  • DateFormat.SHORT 3/27/03
  • DateFormat.MEDIUM Mar 27, 2018
  • DateFormat.LONG March 27, 2018
  • DateFormat.FULL Thursday, March 27, 2018
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    Date today = new Date();

    Locale defaultLocale = Locale.getDefault();
    printDate(defaultLocale, today);//w  w  w . j  ava2  s  .  c o  m

  }
  public static void printDate(Locale locale, Date date) {
    DateFormat formatter;
    String formattedDate;

    // Format and print the date in SHORT style
    formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    formattedDate = formatter.format(date);
    System.out.println("SHORT: " + formattedDate);

    // Format and print the date in MEDIUM style
    formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
    formattedDate = formatter.format(date);
    System.out.println("MEDIUM: " + formattedDate);

    formatter = DateFormat.getDateInstance(DateFormat.LONG, locale);
    formattedDate = formatter.format(date);
    System.out.println("LONG: " + formattedDate);
  }
}



PreviousNext

Related