This section provides a tutorial example on how to use java.util.DateFormat and java.util.SimpleDateFormat classes to convert or format data objects into text strings in a specific calendar in various locales.
In the previous chapter, we learned how to use java.util.Date, java.util.Calendar and related classes
to manage date, time and calendar information as objects or numeric values.
If you want to convert or format date, time and calendar information into text strings
in different human languages and styles, you need to learn 2 more JDK classes.
The DateFormat class, java.text.DateFormat, is an abstract class providing a
foundation for subclasses to present date objects in a specific calendar in text strings,
and parsing text string back to date objects.
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.