Convert String to Date in Java
Given a String in date format, the task is to convert this String into actual Date.
Examples:
Input: string = "2018-10-28T15:23:01Z" Output: 2018-10-28T15:23:01Z Input: string = "28 October, 2018" Output: 2018-10-28
- Method 1: Using Instant Class. Instant class in java.time package gives nanosecond accuracy. It is similar to Date class but gives better accuracy.
Approach:
- Get the String to be converted.
- Create an empty Instant timestamp object
- Covnert the String to Date using Instant.parse() method.
- If converted successfully, then print the Date
- If not converted successfully, then DateTimeParseException is thrown.
Below is the implementation of the above approach:
// Java program to convert String to Dateimportjava.time.Instant;importjava.time.format.DateTimeParseException;classGFG {// Function tot convert String to DatepublicstaticInstant getDateFromString(String string){// Create an Instant objectInstant timestamp =null;// Parse the String to Datetimestamp = Instant.parse(string);// return the converted timestampreturntimestamp;}publicstaticvoidmain(String[] args){// Get the StringString string ="2018-10-28T15:23:01Z";// Get the Date from Stringtry{Instant timestamp = getDateFromString(string);// Print the converted DateSystem.out.println(timestamp);}// Throws DateTimeParseException// if the string cannot be parsedcatch(DateTimeParseException e) {System.out.println("Exception: "+ e);}}}chevron_rightfilter_noneOutput:2018-10-28T15:23:01Z
- Method 2: Using DateTimeFormatter Class.
Approach:
- Get the String to be converted and the required format.
- Create an empty LocalDate object
- Covnert the String to Date using LocalDate.parse() method.
- If converted successfully, then print the Date
- If the String pattern is invalid, then IllegalArgumentException is thrown.
- If not converted successfully, then DateTimeParseException is thrown.
Below is the implementation of the above approach:
// Java program to convert String to Dateimportjava.time.LocalDate;importjava.time.format.DateTimeFormatter;importjava.time.format.DateTimeParseException;classGFG {// Function tot convert String to DatepublicstaticLocalDategetDateFromString(String string, DateTimeFormatter format){// Convert the String to Date in the specified formatLocalDate date = LocalDate.parse(string, format);// return the converted datereturndate;}publicstaticvoidmain(String[] args){// Get the StringString string ="28 October, 2018";// Get the FormatDateTimeFormatter format = DateTimeFormatter.ofPattern("d MMMM, yyyy");try{// Get the Date from StringLocalDate date = getDateFromString(string, format);// Print the converted dateSystem.out.println(date);}// If the String pattern is invalidcatch(IllegalArgumentException e) {System.out.println("Exception: "+ e);}// If the String was unable to be parsed.catch(DateTimeParseException e) {System.out.println("Exception: "+ e);}}}chevron_rightfilter_noneOutput:2018-10-28
Recommended Posts:
- Program to Convert Milliseconds to a Date Format in Java
- Convert a List of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Convert a Set of String to a comma separated String in Java
- Convert Set of String to Array of String in Java
- Convert timestamp to readable date/time in PHP
- Convert String to Double in Java
- Program to Convert Set of Integer to Set of String in Java
- Program to convert String to IntStream in Java
- Convert List of Characters to String in Java
- Program to convert set of String to set of Integer in Java
- Convert a String to a List of Characters in Java
- Program to convert IntStream to String in Java
- Convert Java Object to Json String using GSON
- Convert String into comma separated List in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



