Java Reference
In-Depth Information
Table 13-1. Predefined Date Format Styles and Formatted Text for Locale as United States
Style
Formatted Date
DEFAULT
Mar 27, 2003
SHORT
3/27/03
MEDIUM
Mar 27, 2003
LONG
March 27, 2003
FULL
Thursday, March 27, 2003
The program listed in Listing 13-1 displays dates in short and medium formats for locales as default (which is US
for the JVM running this example), France, and Germany. The program prints the current date. It will print different
formats of the same date when you run this program. Your results will be different than those displayed since it
displays different formats for today's date.
Listing 13-1. Using the Predefined Date Formats
// PredefinedDateFormats.java
package com.jdojo.format;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class PredefinedDateFormats {
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);
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();
 
Search WWH ::




Custom Search