Java Program to Convert String to Date
Last Updated :
01 Mar, 2023
Given a string in date format, the task is to convert this String into an actual date. Here the main concept is the parse() method which helps in the conversion.
Illustration:
Input : string = "2018-10-28T15:23:01Z"
Output: 2018-10-28T15:23:01Z
Input : string = "28 October, 2018"
Output: 2018-10-28
Different Methods to Convert String to Date
- Using instant class
- Using DateTimeFormatter class
- Using SimpleDateFormat class
Tip: Must Read the articles DateFormat class and SimpleDateFormat Class.
Now let us discuss the above method one by one that is as follows:
Method 1: Using Instant Class
Instant class in java.time package gives nanosecond accuracy. It is similar to the Date class but gives better accuracy.
Approach:
- Get the String to be converted.
- Create an empty Instant timestamp object.
- Convert the String to Date using the Instant.parse() method.
- If converted successfully, then print the Date.
- If not converted successfully, then DateTimeParseException is thrown.
Example
Java
import java.time.Instant;
import java.time.format.DateTimeParseException;
class GFG {
public static Instant getDateFromString(String string)
{
Instant timestamp = null ;
timestamp = Instant.parse(string);
return timestamp;
}
public static void main(String[] args)
{
String string = "2018-10-28T15:23:01Z" ;
try {
Instant timestamp = getDateFromString(string);
System.out.println(timestamp);
}
catch (DateTimeParseException e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
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.
- Convert 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.
Example:
Java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
class GFG {
public static LocalDate
getDateFromString(String string,
DateTimeFormatter format)
{
LocalDate date = LocalDate.parse(string, format);
return date;
}
public static void main(String[] args)
{
String string = "28 October, 2018" ;
DateTimeFormatter format
= DateTimeFormatter.ofPattern( "d MMMM, yyyy" );
try {
LocalDate date
= getDateFromString(string, format);
System.out.println(date);
}
catch (IllegalArgumentException e) {
System.out.println( "Exception: " + e);
}
catch (DateTimeParseException e) {
System.out.println( "Exception: " + e);
}
}
}
|
Method 3: Using SimpleDateFormat class
Approach:
- Taking string input and storing it into a string
- Creating an object of Date class with reference to SimpleDateFormat class
- parsing date format into it.
- Print the corresponding date.
Example:
Java
import java.text.SimpleDateFormat;
import java.util.Date;
class GFG {
public static void main(String[] args) throws Exception
{
String strDate = "29/12/96" ;
Date date = new SimpleDateFormat( "dd/mm/yyyy" )
.parse(strDate);
System.out.print(strDate + " " + date);
}
}
|
Output
29/12/96 Fri Jan 29 00:12:00 UTC 96
Output: Also when. run on the terminal, it is as follows:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...