In this post we’ll see how to convert String to Date in Java.
For converting Date to String in Java check this post- Convert Date to String in Java
Before Java 8, SimpleDateFormat was the option in Java for converting String to Date. Java 8 onward you can use classes in package java.time which are part of new date and time API for the conversion. We’ll see examples using methods of both of these classes.
Converting String to Date using SimpleDateFormat
You can use parse() method of the Java SimpleDateFormat class that parses text from a string to produce a Date.
First thing is to create an instance of SimpleDateFormat passing the date and time pattern for parsing. Then call parse() method passing the date String, method returns parsed Date. ParseException is thrown if unable to parse the requested result.
In the example different types of date Strings are converted to java.util.Date instances.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ParseDate { public static void main(String[] args) { try { parseDT("dd/MM/yyyy", "09/08/2019"); parseDT("MM-dd-yyyy", "09-08-2019"); // Date will default to epoch (January 1, 1970) parseDT("HH:mm:ss", "20:04:19"); parseDT("MM-dd-yyyy HH:mm:ss", "09-08-2019 20:04:19"); }catch (ParseException e) { System.out.println("Error while parsing- " + e.getMessage()); } } private static void parseDT(String pattern, String dateTime) throws ParseException{ // Create date format as per specified pattern SimpleDateFormat sdf = new SimpleDateFormat(pattern); // parsing Date dt = sdf.parse(dateTime); System.out.println("After parsing- " + dt); } }
Output
After parsing- Fri Aug 09 00:00:00 IST 2019 After parsing- Sun Sep 08 00:00:00 IST 2019 After parsing- Thu Jan 01 20:04:19 IST 1970 After parsing- Sun Sep 08 20:04:19 IST 2019
Converting String to Date using new Date & Time API
Java 8 onward you can use parse() method of the LocalDate (representing date), LocalTime (representing time) and LocalDateTime (representing date and time) to convert String to Date.
There are two variants of parse method-
- parse(CharSequence text)– Here text parameter represents the date string that has to be parsed. String must represent a valid date, time or date-time
- parse(CharSequence text, DateTimeFormatter formatter) –You can pass an instance of DateTimeFormatter representing formatter to be used for parsing.
Converting String to LocalDate
LocalDate dt = LocalDate.parse("2019-08-03");// Date in ISO-8601 format
Converting String to LocalTime
LocalTime dt = LocalTime.parse("10:15:30");// Time in ISO-8601 format
Converting String to LocalDateTime
LocalDateTime dt = LocalDateTime.parse("2007-12-03T10:15:30");// Date-Time in ISO-8601 format
Converting String to ZonedDateTime
If there is time zone information then use ZonedDateTime.
// Date-Time with time zone in ISO-8601 format ZonedDateTime dt = ZonedDateTime.parse("2019-07-03T10:15:30+01:00[Europe/Paris]");
Converting String to Date using custom formatter
In DateTimeFormatter class there is a static method ofPattern() using which you can specify the pattern for date time formatting.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Date; public class ParseDate { public static void main(String[] args) { try{ LocalDate localDate = LocalDate.parse("30/06/2019", DateTimeFormatter.ofPattern("dd/MM/yyyy")); System.out.println("Date " + localDate); localDate = LocalDate.parse("Thu, Sep 19, '19", DateTimeFormatter.ofPattern("EEE, MMM d, ''yy")); System.out.println("Date " + localDate); LocalTime localTime = LocalTime.parse("20:17:46", DateTimeFormatter.ofPattern("HH:mm:ss")); System.out.println("Time " + localTime); //DateTime LocalDateTime localDateTime = LocalDateTime.parse("2019-08-12T20:17:46.384Z", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz")); System.out.println("Date-Time " + localDateTime); // DateTime with zone offset ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-08-18 AD at 10:13:46 PDT", DateTimeFormatter.ofPattern("yyyy-MM-dd G 'at' HH:mm:ss z")); System.out.println("Date " + zonedDateTime); // DateTime with zone offset zonedDateTime = ZonedDateTime.parse("2019-08-15 03:32:12-0430", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssxx")); System.out.println("Date " + zonedDateTime); }catch(DateTimeParseException ex){ ex.printStackTrace(); } } }
Output
Date 2019-06-30 Date 2019-09-19 Time 20:17:46 Date-Time 2019-08-12T20:17:46.384 Date 2019-08-18T10:13:46-07:00[America/Los_Angeles] Date 2019-08-15T03:32:12-04:30
Related Posts
- Convert Date to String in Java
- Convert Instant to Java LocalDateTime, LocalTime
- Convert LocalDate to Date in Java
- Compare Dates in Java
- Java Program to Convert Date to LocalDate, LocalDateTime
- Shell Sort Java Program
- Merging PDFs in Java Using PDFBox
- How to Create a Deadlock in Java
That’s all for the topic Convert String to Date in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like