A Guide to Java's SimpleDateFormat
Formatting dates is always a challenge, but Java's SimpleDataFormat class has plenty of options (and a few pitfalls) that can take the frustration out of dates.
Join the DZone community and get the full member experience.
Join For FreeWe can easily parse and format dates in Java with the SimpleDateFormat class. It is similar to using the POSIX function strftime() in C/C++ with a few gotchas. Let's learn the proper usage of SimpleDateFormat and delve into some of its issues.
Parsing Dates with SimpleDateFormat
Create a SimpleDateFormat instance by specifying the format. (See the table farther down in the article for 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
Formatting Date and Time
Format a date using the pattern specified with format(). Pass in a Dateinstance and generate output in the chosen format. Nothing to it.
System.out.println(dateFormat.format(new Date()));
// prints: 2017-01-17
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
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
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 |
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
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.
Published at DZone with permission of Jay Sridhar, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments