Java SimpleDateFormat Guide

Simple Date Format Examples

1. Introduction

We can parse and format dates in Java easily with the SimpleDateFormat class. It is similar to using the POSIX function strftime() in C/C++ with a few gotchas. Let us learn the proper usage of SimpleDateFormat and delve into some of its issues.

2. Parsing Dates with SimpleDateFormat

A first look at its usage. Create a SimpleDateFormat instance by specifying the format. (See below for a table of all format directives.)

Note that if you need to parse for (or include) literal characters in the format string, you need to quote it with a single quote (\u0027).

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Use it to parse a string into a date as follows:

Date date = dateFormat.parse("2017-01-17");
System.out.println(date);

// prints: Tue Jan 17 00:00:00 UTC 2017

3. Formatting Date and Time

Format a date using the pattern specified with format(). Pass in a Date instance and generate output in the chosen format. Nothing to it.

System.out.println(dateFormat.format(new Date()));
// prints: 2017-01-17

3.1. Using String.format()

Another option when it comes to formatting dates is to use String.format(). It provides most of the formatting directives that SimpleDateFormat does (though requiring some extra characters). Here are some examples:

4-digit year
String str = String.format("%1$tY", new Date());
// prints: 2017
Complete Date
String str = String.format("%1$tY-%1$tm-%1$td", new Date());
// prints: 2017-02-20

// or even:
String str = String.format("%1$tF", new Date());
// prints: 2017-02-20
Date and Time
String str = String.format("%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS", new Date());
// prints: 2017-02-20T17:31:31
With Time Zone
String str = String.format("%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS%1$tz", new Date());
// prints: 2017-02-20T17:31:31-08:00

4. Date and Time Formats

Let us now look at some commonly used formats.

These formats are based on ISO-8601.

4-digit year only
SimpleDateFormat df = new SimpleDateFormat("yyyy");
System.out.println(df.format(new Date()));
// prints: 2017
Year and month
... new SimpleDateFormat("yyyy-MM");
// prints: 2017-02
Complete date
... new SimpleDateFormat("yyyy-MM-dd");
// prints: 2017-02-19
Date and time with hour, minute and time zone
... new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZZZZ");
// prints: 2017-02-19T22:19-0800
Date and time including seconds
... new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ");
// prints: 2017-02-19T22:36:06-0800
Date and time with seconds and decimal fraction of a second
... new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ");
// prints: 2017-02-19T22:37:38.634-0800
Date with week
... new SimpleDateFormat("yyyy-'W'ww");
// prints: 2017-W08
Date with week and day of the week
... new SimpleDateFormat("yyyy-'W'ww-F");
// prints: 2017-W08-3
Date with year and day of the year
... new SimpleDateFormat("yyyy-DDD");
// prints: 2017-050
Time of the day in local time zone
... new SimpleDateFormat("HH:mm:ss");
// prints: 22:52:11
Time of the day with time zone
... new SimpleDateFormat("HH:mm:ssZZZZ");
// prints: 22:55:37-0800

5. Supported Directives

Here is the full list of supported date and time format characters (from the Javadocs).

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year (context sensitive) Month July; Jul; 07
L Month in year (standalone form) Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, …, 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00

6. Change Time Zone

Change the time zone of the date format instance by using the method setTimeZone().

SimpleDateFormat df = new SimpleDateFormat(..);
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

The default time zone comes from the JVM. Change the JVM time zone programmatically as follows:

TimeZone.setDefault(TimeZone.getTimeZone("PST"));

Or change the time zone outside the program by specifying the system property user.timezone.

java -Duser.timezone=EST ...

Want system-wide changes? On ubuntu, this necessitates changing the /etc/localtime file. Do the following (as root):

# dpkg-reconfigure tzdata

7. SimpleDateFormat Thread Safety

The SimpleDateFormat class is not thread safe. This means you cannot use the same instance from multiple threads. Which is a pity because frequently the same date/time format is used within sections of an application. And when these sections are executed by different threads, you cannot use the same instance of SimpleDateFormat.

One solution to this problem is to use the FastDateFormat from Apache Commons Lang. This class is advertised as “thread-safe” and “can be used as a static member.” If you don’t mind adding a dependency on Apache Commons (or if your application already has a dependency), then this is the way to go.

Summary

SimpleDateFormat is really useful when it comes to parsing and formatting dates. It offers a variety of formatting directives covering all aspects of dealing with dates and times. However the class is not thread-safe and special arrangements have to be made to use it multi-threaded situations.

Leave a Reply

Your email address will not be published. Required fields are marked *