Java Reference
In-Depth Information
TRY IT OUT: Producing Dates and Times
This example shows the four different date formats for four countries:
// Trying date formatting
import java.util.Locale;
import java.text.DateFormat;
import java.util.Date;
import static java.util.Locale.*;
// Import names of constants
import static java.text.DateFormat.*;
// Import names of constants
public class TryDateFormats {
public enum Style {FULL, LONG, MEDIUM, SHORT}
public static void main(String[] args) {
Date today = new Date();
Locale[] locales = {US, UK, GERMANY, FRANCE};
// Output the date for each locale in four styles
DateFormat fmt = null;
for(Locale locale : locales) {
System.out.println("\nThe Date for " +
locale.getDisplayCountry() + ":");
for (Style style : Style.values()) {
fmt = DateFormat.getDateInstance(style.ordinal(), locale);
System.out.println( " In " + style +
" is " + fmt.format(today));
}
}
}
}
TryDateFormats.java
When I compiled and ran this it produced the following output:
The Date for United States:
In FULL is Monday, June 27, 2011
In LONG is June 27, 2011
In MEDIUM is Jun 27, 2011
In SHORT is 6/27/11
The Date for United Kingdom:
In FULL is Monday, 27 June 2011
In LONG is 27 June 2011
In MEDIUM is 27-Jun-2011
In SHORT is 27/06/11
Search WWH ::




Custom Search