How to convert string to date in Java
In this tutorial, we will learn how to convert a string to date in Java. There are many ways in which we can convert a string into a date in Java using different classes. Here we are going to discuss only two classes and they are LocalDate and SimpleDateFormat.
Using SimpleDateFormat class:
Now, we are going to discuss how to convert a string to date using SimpleDateFormat class.
Firstly we need to understand that a normal string cannot be converted to a date. Therefore we have to write the string in the format of the date.
Like if we write
string date="05March2022";
This cannot be converted into a date. Therefore if the date is written in the correct format then by creating an object of simpledateformat we can convert it into a date.
Like:
string date = "05-03-2022";
After giving the string as input for converting it into a date we will create an object of simpleDateFormat and provide the format of the date to be printed as a parameter. This class will format the string as the pattern of the date given in the parameter. And then we will parse the date.
SimpleDateFormat d = new SimpleDateFormat("dd-MM-yyyy"); System.out.println(d.parse(date));
We will include the packages necessary packages i.e. java. text.ParseException and java.text.SimpleDateFormat and then using scanner class we take the string as user input.
The complete code:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class string_Date { public static void main(String args[]) throws ParseException { Scanner sc = new Scanner(System.in); SimpleDateFormat d = new SimpleDateFormat("dd-MM-yyyy"); String date; System.out.println("Enter the string in this form: dd-MM-yyyy"); date = sc.nextLine(); System.out.println(d.parse(date)); } }
This will also display the time if provided but here we will only learn about the date. So, the time will be 00:00:00.
Output:
Enter the string in this form: dd-MM-yyyy
05-03-2022
Sat Mar 05 00:00:00 IST 2022
Using LocalDate class
Now we are going to discuss how to convert a string to date using the LocalDate class.
We will take the input in the format of how the date is represented.
string date="2022-03-06";
We will convert the string by creating an object of the class LocalDate and providing the format of the date as a parameter and will also parse the date at the same time.
In this way:
LocalDate d = LocalDate.parse(date);
We will include these required packages: java. time.LocalDate and java.util.* and then using scanner we will take the string as user input.
The complete code:
import java.time.LocalDate; import java.util.*; public class string_date1 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String date; System.out.println("Enter the string to be converted to date"); date = sc.nextLine(); LocalDate d = LocalDate.parse(date); System.out.println(d); } }
After executing the code we will get the following output:
Enter the string to be converted to date
2022-03-06
2022-03-06
Hope this tutorial was useful.
Leave a Reply