Locale date time Format

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

public class DateFormatDemo {

  static public void displayDate(Locale currentLocale) {

    Date today;
    String dateOut;
    DateFormat dateFormatter;

    dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,currentLocale);
    today = new Date();
    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();
    System.out.println("Locale: " + currentLocale.toString());
    System.out.println();

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

  }
}
  

Output:


6 sept. 2002   fr_FR
06.09.2002   de_DE
Sep 6, 2002   en_US

Locale: en_US

Sep 6, 2002
9/6/12
Sep 6, 2002
September 6, 2002
Thursday, September 6, 2002

Locale: fr_FR

6 sept. 2002
06/09/12
6 sept. 2002
6 septembre 2002
jeudi 6 septembre 2002

Locale: en_US

9:00:40 AM
9:00 AM
9:00:40 AM
9:00:40 AM PDT
9:00:40 AM PDT

Locale: de_DE

09:00:40
09:00
09:00:40
09:00:40 PDT
09:00 Uhr PDT

Locale: en_US

Sep 6, 2002 9:00:40 AM
9/6/12 9:00 AM
Sep 6, 2002 9:00:40 AM
September 6, 2002 9:00:40 AM PDT
Thursday, September 6, 2002 9:00:40 AM PDT

Locale: fr_FR

6 sept. 2002 09:00:40
06/09/12 09:00
6 sept. 2002 09:00:40
6 septembre 2002 09:00:40 PDT
jeudi 6 septembre 2002 09 h 00 PDT
Home 
  Java Book 
    Runnable examples  

Date Format:
  1. Default date format
  2. Full date format
  3. Long Date Format
  4. Medium Locale Canada time format
  5. Short date Format
  6. Short Time Format
  7. Pattern date Format
  8. Format date(a): am/pm marker
  9. Format date(d): day in number
  10. Format date(dd): day in two digits number format
  11. Format date(E):Day of week
  12. Format date(EEEE): full name of day of week
  13. Format date format(h):hour in number
  14. Format date(hh): hour in two digits number
  15. Format date(H):hour in a day 24 hour format (0-23)
  16. Format date(HH):hour in a day two digit(0-23)
  17. Format date(k):hour in a day(1-24)
  18. Format date(kk):hour in a day(1-24) two digits
  19. Format date(K):hour in a day (0-11 in AM/PM)
  20. Format date(KK): hour in a day (00-11)
  21. Format date(m): minute
  22. Format data component(mm):minute in two digit(01, 02 etc.)
  23. Format date(M):month in 1-12
  24. Format date(MM):month in two digit( 01, 02 etc.)
  25. Format date(MMM): month in short name(Jan, Feb etc.)
  26. Format date(MMMM): month in full name(January, February etc.)
  27. Format date(s): second
  28. Format date(ss): second in two digit(01, 02 etc.)
  29. Format date(yy):year in two digit format( 07, 08 etc.)
  30. Format date(yyyy):year in full four digit(2007, 2008 etc.)
  31. Format date(z):time zone
  32. Format date(Z): time zone in offset
  33. Format date(zzzz): time zone in full description
  34. Format date:hh:mm:ss a
  35. Format date:HH.mm.ss
  36. Format date:HH:mm:ss Z
  37. Format date(HH:mm:ss:SSS)
  38. Format date:MM/dd/yy
  39. Format date:mm-dd-yyyy hh:mm:ss
  40. Format date:dd-MMM-yy
  41. format date:dd MMM yyyy hh:mm:ss a
  42. Format Date:EEEE MMMMM dd yyyy kk:mm:ss
  43. Format date:E, dd MMM yyyy HH:mm:ss Z
  44. Format date:YYYYMMDD
  45. Locale available for date format
  46. Locale date format
  47. Locale date time Format
  48. Locale Canada Default date format
  49. Locale Canada Default time format
  50. Locale Canada Full time format
  51. Locale Canada Long time format
  52. Date Format pattern string
  53. Month Name List
  54. Short Month Name List
  55. Weekday Name List
  56. Weekday Short Name List
  57. Change date format symbols
  58. Date Format Symbol
  59. Current default date format
  60. Format a date using Rfc822 Date Format