Customizing Formats

Version note:

This Date and Time section uses the date and time APIs in the java\.util package. The java\.time APIs, available in the JDK 8 release, provides a comprehensive date and time model that offers significant improvements over the java\.util classes. The java\.time APIs are described in the Date Time trail. The Legacy Date-Time Code page might be of particular interest.

The previous section, Using Predefined Formats, described the formatting styles provided by the DateFormat class. In most cases these predefined formats are adequate. However, if you want to create your own customized formats, you can use the SimpleDateFormat class.

The code examples that follow demonstrate the methods of the SimpleDateFormat class. You can find the full source code for the examples in the file named SimpleDateFormatDemo.

About Patterns

When you create a SimpleDateFormat object, you specify a pattern String . The contents of the pattern String determine the format of the date and time. For a full description of the pattern's syntax, see the tables in Date Format Pattern Syntax.

The following code formats a date and time according to the pattern String passed to the SimpleDateFormat constructor. The String returned by the format method contains the formatted date and time that are to be displayed.

Date today;
String output;
SimpleDateFormat formatter;

formatter = new SimpleDateFormat(pattern, currentLocale);
today = new Date();
output = formatter.format(today);
System.out.println(pattern + " " + output);

The following table shows the output generated by the previous code example when the U.S. Locale is specified:

Customized Date and Time Formats

PatternOutput
dd.MM.yy30.06.09
yyyy.MM.dd G 'at' hh:mm:ss z2009.06.30 AD at 08:29:36 PDT
EEE, MMM d, ''yyTue, Jun 30, '09
h:mm a8:29 PM
H:mm8:29
H:mm:ss:SSS8:28:36:249
K:mm a,z8:29 AM,PDT
yyyy.MMMMM.dd GGG hh:mm aaa2009.June.30 AD 08:29 AM

Patterns and Locale

The SimpleDateFormat class is locale-sensitive. If you instantiate SimpleDateFormat without a Locale parameter, it will format the date and time according to the default Locale . Both the pattern and the Locale determine the format. For the same pattern, SimpleDateFormat may format a date and time differently if the Locale varies.

In the example code that follows, the pattern is hardcoded in the statement that creates the SimpleDateFormat object:

Date today;
String result;
SimpleDateFormat formatter;

formatter = new SimpleDateFormat("EEE d MMM yy", currentLocale);
today = new Date();
result = formatter.format(today);
System.out.println("Locale: " + currentLocale.toString());
System.out.println("Result: " + result);

When the currentLocale is set to different values, the preceding code example generates this output:

Locale: fr_FR
Result: mar. 30 juin 09
Locale: de_DE
Result: Di 30 Jun 09
Locale: en_US
Result: Tue 30 Jun 09

Date Format Pattern Syntax

You can design your own format patterns for dates and times from the list of symbols in the following table:

SymbolMeaningPresentationExample
Gera designatorTextAD
yyearNumber2009
Mmonth in yearText & NumberJuly & 07
dday in monthNumber10
hhour in am/pm (1-12)Number12
Hhour in day (0-23)Number0
mminute in hourNumber30
ssecond in minuteNumber55
SmillisecondNumber978
Eday in weekTextTuesday
Dday in yearNumber189
Fday of week in monthNumber2 (2nd Wed in July)
wweek in yearNumber27
Wweek in monthNumber2
aam/pm markerTextPM
khour in day (1-24)Number24
Khour in am/pm (0-11)Number0
ztime zoneTextPacific Standard Time
'escape for textDelimiter(none)
'single quoteLiteral'

Characters that are not letters are treated as quoted text. That is, they will appear in the formatted text even if they are not enclosed within single quotes.

The number of symbol letters you specify also determines the format. For example, if the "zz" pattern results in "PDT," then the "zzzz" pattern generates "Pacific Daylight Time." The following table summarizes these rules:

PresentationNumber of SymbolsResult
Text1 - 3abbreviated form, if one exists
Text>= 4full form
Numberminimum number of digits is requiredshorter numbers are padded with zeros (for a year, if the count of 'y' is 2, then the year is truncated to 2 digits)
Text & Number1 - 2number form
Text & Number3text form