Java Data Type How to - Do locale based date format








Question

We would like to know how to do locale based date format.

Answer

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
//  w  ww.  ja v a2  s .c  om
public class Main {
  public static void main(String args[]) {
    int style = DateFormat.MEDIUM;
    // Also try with style = DateFormat.FULL and DateFormat.SHORT
    Date date = new Date();
    DateFormat df;
    df = DateFormat.getDateInstance(style, Locale.UK);
    System.out.println("United Kingdom: " + df.format(date));
    df = DateFormat.getDateInstance(style, Locale.US);
    System.out.println("USA: " + df.format(date));
    df = DateFormat.getDateInstance(style, Locale.FRANCE);
    System.out.println("France: " + df.format(date));
    df = DateFormat.getDateInstance(style, Locale.ITALY);
    System.out.println("Italy: " + df.format(date));
    df = DateFormat.getDateInstance(style, Locale.JAPAN);
    System.out.println("Japan: " + df.format(date));
  }
}

The code above generates the following result.