Java I18N: Format : Date Format : Date Format « I18N « Java






Java I18N: Format : Date Format

Java I18N: Format : Date Format
 
/* From http://java.sun.com/docs/books/tutorial/index.html */
/*
 * Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for NON-COMMERCIAL purposes and without fee is hereby granted
 * provided that this copyright notice appears in all copies. Please refer to
 * the file "copyright.html" for further important copyright and licensing
 * information.
 * 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

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

  }
}

           
         
  








Related examples in the same category

1.Show DateShow Date
2.Simple Date formatSimple Date format
3.Java I18N: Format : Date Format Symbols DemoJava I18N: Format : Date Format Symbols Demo
4.Demonstrate number and date internationalizationDemonstrate number and date internationalization
5.Use some locales choicesUse some locales choices
6.Format some values using the default locale
7.Formatting and Parsing a Time for a Locale Using Default Formats
8.Formatting and Parsing a Time for a Locale: specify the locale when creating the SimpleDateFormat object.
9.Formatting and Parsing a Date for a Locale: specify the locale when creating the SimpleDateFormat object.