Java - Legacy Date Time Formatting

Introduction

Java library provides two classes to format legacy dates:

java.text.DateFormat
java.text.SimpleDateFormat

Use the DateFormat class to format dates using a predefined format.

It is an abstract class. You can call one of its getXxxInstance() methods, where Xxx can be Date, DateTime, or Time, to get the formatter object, or just getInstance().

The formatted text depends on two things: style and locale.

Use the format() method of the DateFormat class to format a date and time.

The style of formatting determines how much datetime information is included in the formatted text.

Locale determines how all pieces of information are assembled.

Style

The DateFormat class defines five styles as constants:

  • DateFormat.DEFAULT
  • DateFormat.SHORT
  • DateFormat.MEDIUM
  • DateFormat.LONG
  • DateFormat.FULL

DEFAULT format is the same as MEDIUM, unless you use getInstance() where the default is SHORT.

The following table shows the same date formatted in different styles for a US locale.

Style Formatted Date
DEFAULT Mar 27, 2018
SHORT 3/27/03
MEDIUMMar 27, 2018
LONG March 27, 2018
FULL Thursday, March 27, 2018

Demo

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

public class Main {
  public static void main(String[] args) {
    // Get the current date
    Date today = new Date();

    // Print date in the default locale format
    Locale defaultLocale = Locale.getDefault();
    printLocaleDetails(defaultLocale);/* ww w  .j a  v  a 2s .co m*/
    printDate(defaultLocale, today);

    // Print date in French format
    printLocaleDetails(Locale.FRANCE);
    printDate(Locale.FRANCE, today);

    // Print date in German format. 
    // We could also use Locale.GERMANY
    // instead of new Locale ("de", "DE").
    Locale germanLocale = new Locale("de", "DE");
    printLocaleDetails(germanLocale);
    printDate(germanLocale, today);
  }

  public static void printLocaleDetails(Locale locale) {
    String languageCode = locale.getLanguage();
    String languageName = locale.getDisplayLanguage();
    String countryCode = locale.getCountry();
    String countryName = locale.getDisplayCountry();

    // Print the locale info
    System.out.println("Language: " + languageName + "(" + languageCode + "); "
        + "Country: " + countryName + "(" + countryCode + ")");
  }

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

    // Print a blank line at the end
    System.out.println();
  }
}

Result

Locale

java.util.Locale class contains constants for some common locales.

You can use Locale.FRANCE for a locale with language "fr" and country code "FR".

Locale frenchLocale = new Locale("fr", "FR") ;

To create a Locale, use a two-letter lowercase language code and a two-letter uppercase country code if the Locale class does not declare a constant for that country.

Language codes and country codes have been listed in ISO-639 code and ISO-3166 code.

Some more examples of creating locales are as follows:

Locale hindiIndiaLocale = new Locale("hi", "IN");
Locale bengaliIndiaLocale = new Locale("bn", "IN");
Locale thaiThailandLocale = new Locale("th", "TH");

You can use the Locale.getDefault() method to get the default  Locale for your system.

Exercise