|
Formatting and Parsing Dates
Part:
1
2
Sample programs listed in this chapter have been tested with JDK 1.3.1, 1.4.1 and 1.5.0.
Formatting Dates to Strings
The date and calendar classes manage date, time and calendar related
information in numeric values. JDK provides two classes to convert
those values into text in different languages and styles.
The DateFormat class, java.text.DateFormat, is an abstract class providing a
fundation for subclasses to present time in a specific calendar in text,
and parsing text back to time.
The SimpleDateFormat class, java.text.SimpleDateFormat, is concrete subclass of
the DateFormat class. It can be customized with different formatting patterns
and in different locales.
Once a DateFormat object is created with a predefined pattern or a customized pattern,
it can be used to convert a Date object into a string based on the pattern using the
format() method.
The following program shows how to create DateFormat objects and how to use them:
import java.util.*;
import java.text.*;
class DateFormatTest {
public static void main(String[] a) {
showDateFormat();
showSimpleDateFormat();
}
public static void showDateFormat() {
Date now = new Date(); // the current time
System.out.println("Date toString() format: " + now.toString());
DateFormat df = DateFormat.getInstance();
System.out.println("Default DateFormat: " + df.format(now));
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println("Default DateFormat in west coast: "
+ df.format(now));
df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT, Locale.US);
System.out.println("Short date time in US: " + df.format(now));
df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM, Locale.US);
System.out.println("Medium date time in US: " + df.format(now));
df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT, Locale.FRENCH);
System.out.println("Short date time in French: "
+ df.format(now));
df = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG, Locale.FRENCH);
System.out.println("Long date time in French: "+df.format(now));
}
public static void showSimpleDateFormat() {
Date now = new Date(); // the current time
System.out.println("Date toString() format: " + now.toString());
SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println("Default SimpleDateFormat: " +sdf.format(now));
sdf = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println("The FDA standard format: " + sdf.format(now));
sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
System.out.println("Date.toString() format: " + sdf.format(now));
sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",
Locale.FRENCH);
System.out.println("Date.toString() format in French: "
+ sdf.format(now));
}
}
Output:
Date toString() format: Mon Oct 21 20:54:05 EDT 2002
Default DateFormat: 10/21/02 8:54 PM
Default DateFormat in west coast: 10/21/02 5:54 PM
Short date time in US: 10/21/02 8:54 PM
Medium date time in US: Oct 21, 2002 8:54:05 PM
Short date time in French: 21/10/02 20:54
Long date time in French: 21 octobre 2002 20:54:05 EDT
Date toString() format: Mon Oct 21 20:54:05 EDT 2002
Default SimpleDateFormat: 10/21/02 8:54 PM
The FDA standard format: 21-Oct-2002
Date.toString() format: Mon Oct 21 20:54:05 EDT 2002
Date.toString() format in French: lun. oct. 21 20:54:05 EDT 2002
Note that the DateFormat class is providing predefined formats for a number
of commonly used styles in different locales. The predefined formats are accessible
by the get*Instance() methods.
Note also that the DateFormat class uses GregorianCalendar as the default calendar.
(Continued on next part...)
Part:
1
2
|