Convert String to Date using SimpleDateFormat Class in Java
This post will discuss how to convert a string to date using SimpleDateFormat class in Java.
SimpleDateFormat class is used for formatting (date -> string) and parsing (string -> date) dates. SimpleDateFormat allows us to define our own date and time pattern strings for date-time formatting using the following pattern letters:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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 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 in week Text Tuesday; Tue u Day number of week 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 PST; GMT-08:00 Z Time zone RFC 822 time zone -0800 X Time zone ISO 8601 time zone -08; -0800; -08:00 |
Pattern letters are usually repeated, as their count determines the exact representation. To illustrate, consider the following examples showing how date and time patterns are interpreted for 'Saturday, October 08, 2016, 08:18:30 AM, Eastern Daylight Time'.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Date and Time Pattern Text -------------------------------------------------------------------------- MM/dd/yyyy 10/08/2016 d MMMM, yyyy 8 October, 2016 E, MMM dd yyyy Sat, Oct 08 2016 EEEE, MMMM dd, yyyy HH:mm:ss a Saturday, October 08, 2016 08:18:30 AM yyyy.MM.dd G 'at' HH:mm:ss z 2016.10.08 AD at 08:18:30 EDT EEE, MMM d, ''yy Sat, Oct 8, '16 h:mm a 8:18 AM hh 'o''clock' a, zzzz 08 o'clock AM, Eastern Daylight Time K:mm a, z 8:18 AM, EDT EEE, d MMM yyyy HH:mm:ss Z Sat, 8 Oct 2016 08:18:30 -0400 yyMMddHHmmssZ 161008081830-0400 yyyy-MM-dd'T'HH:mm:ss.SSSZ 2016-10-08T08:18:30.987-0400 yyyy-MM-dd'T'HH:mm:ss.SSSXXX 2016-10-08T08:18:30.987-04:00 YYYY-'W'ww-u 2016-W41-7 |
Note that the patterns are case-sensitive, and text-based patterns of 4 characters or more represent the full form. For example, consider the input string October 8, 2016, we can use MMMM d, yyyy for it. Since October is a full-text month, the MMMM pattern is used for it. Similarly, the d pattern is used for the day in the month 8 and yyyy pattern for 2016, the 4-digit year.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; // Convert `String` to `Date` in Java using `SimpleDateFormat` class Main { public static void main(String[] args) { String string = "October 8, 2016"; DateFormat format = new SimpleDateFormat("MMMM d, yyyy"); try { Date date = format.parse(string); System.out.println(date); } catch (ParseException e) { e.printStackTrace(); } } } |
Output:
Sat Oct 08 00:00:00 GMT 2016
That’s all about converting String to Date using the SimpleDateFormat class in Java.
Also See:
Reference: SimpleDateFormat (Java Platform SE 8)
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)