How to Convert String to Date in Java
Java Strings
- A Java object that represents a string of characters is called a string. Strings are frequently employed to store text data like names, addresses, words, etc. A String class that comes with Java offers methods for comparing and manipulating strings.
- Java has a special memory area called the string pool, where string objects are stored. If two strings have the same value, only one object will be created and stored in the string pool.
To know more about Java Program to Convert String to Date, read the complete article.
Steps to Convert String to Date in Java
To convert a string to a date in Java, you can follow these steps:
Import the java.text.SimpleDateFormat class in your Java program.
Create an instance of the SimpleDateFormat class with a desired date format pattern.
Use the parse() method of the SimpleDateFormat class to parse the string into a Date object.
If necessary, use the getTime() method of the Date class to get the time in milliseconds.
Let’s look at a Java Program to Convert String to Date to perform certain operations.
Example 1: Java Program to Convert String to Date.
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { String dateString = "2023-02-08"; DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; LocalDate date = LocalDate.parse(dateString, formatter); System.out.println("Date: " + date); } }
Output
Date: 2023-02-08
- This code uses the LocalDate class from the java.time package to represent the date, and the DateTimeFormatter class to parse the string into a date. In this example, the ISO_LOCAL_DATE formatter is used, which has the format yyyy-MM-dd.
Example 2 : Java Program to Convert String to Date using predefined formatters
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { String dateString = "2023-02-08"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = formatter.parse(dateString); System.out.println("Date: " + date); } catch (ParseException e) { System.out.println("Exception while parsing the date: " + e.getMessage()); } } }
Output
Date: Wed Feb 08 00:00:00 GMT 2023
Example 3: Java Program Convert String to Date using pattern formatters
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { String dateString = "07-Feb-2023"; SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); try { Date date = formatter.parse(dateString); System.out.println("Date: " + date); } catch (ParseException e) { System.out.println("Exception while parsing the date: " + e.getMessage()); } } }
Output
Date: Tue Feb 07 00:00:00 GMT 2023
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment