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:

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

Download  Run Code

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:

Convert String to Date in Java 8 and above

 
Reference: SimpleDateFormat (Java Platform SE 8)