Using the Predefined Date Formats - Java Date Time

Java examples for Date Time:Legacy Date Format

Description

Using the Predefined Date Formats

Demo Code

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);/*www.ja  v  a  2  s.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();
  }
}

Related Tutorials