Date Format with Locale in Java

Description

The following code shows how to date Format with Locale.

Example


/*from   w  w  w .  j  a v  a  2  s  .c o  m*/
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {

  static public void displayDate(Locale currentLocale) {
    Date today = new Date();
    DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,currentLocale);
    String dateOut = dateFormatter.format(today);
    System.out.println(dateOut + "   " + currentLocale.toString());
  }

  static public void showBothStyles(Locale currentLocale) {

    Date today;
    String result;
    DateFormat formatter;

    int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM,
        DateFormat.LONG, DateFormat.FULL };

    System.out.println("Locale: " + currentLocale.toString());
    today = new Date();

    for (int k = 0; k < styles.length; k++) {
      formatter = DateFormat.getDateTimeInstance(styles[k], styles[k],
          currentLocale);
      result = formatter.format(today);
      System.out.println(result);
    }
  }

  static public void showDateStyles(Locale currentLocale) {

    Date today = new Date();
    String result;
    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);
    }
  }

  static public void showTimeStyles(Locale currentLocale) {

    Date today = new Date();
    String result;
    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.getTimeInstance(styles[k], currentLocale);
      result = formatter.format(today);
      System.out.println(result);
    }
  }

  static public void main(String[] args) {

    Locale[] locales = { new Locale("fr", "FR"), new Locale("de", "DE"),
        new Locale("en", "US") };

    for (int i = 0; i < locales.length; i++) {
      displayDate(locales[i]);
    }

    showDateStyles(new Locale("en", "US"));
    showDateStyles(new Locale("fr", "FR"));

    showTimeStyles(new Locale("en", "US"));
    showTimeStyles(new Locale("de", "DE"));

    showBothStyles(new Locale("en", "US"));
    showBothStyles(new Locale("fr", "FR"));

  }
}




















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone