Convert String to Date using SimpleDateFormat Class in Java

In this post, we will see 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 following pattern letters:

 

 

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'.

 

 
Note that the patterns are case sensitive and text based patterns of 4 characters or more represents the full form. For example, consider the input string October 8, 2016, we can use MMMM d, yyyy for it. Since October is full text month, MMMM pattern is used for it. Similarly, d pattern is used for day in month 8 and yyyy pattern for 2016 which is the 4-digit year.

 

Download   Run Code

Output:

Sat Oct 08 00:00:00 GMT 2016

 

 
Also See: Convert String to Date in Java 8

 
References: SimpleDateFormat (Java Platform SE 8)

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz