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.
Java String :Java provides a mutable alternative to the String class, called StringBuilder, for use in situations where strings need to be dynamically constructed or modified.
Java Basic Input and Output : The idea of Java input and output is crucial while learning to develop in Java. It includes components like input, output, and stream. The data we provide to the software is considered the input. The data we obtain from the programme as a result is known as the output. Data flow or sequence are represented by streams. We employ the input stream to provide input, and the output stream to provide output.
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
Explanation:
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
Explanation:The SimpleDateFormat class is used to parse the string into a date. The SimpleDateFormat constructor takes a format string as its argument, which specifies the expected format of the input string. The parse method of the SimpleDateFormat class is then used to parse the string into a Date object. If the string doesn't match the expected format, a ParseException is thrown.
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
Explanation:
A custom pattern is specified in the formatter, dd-MMM-yyyy, which represents the day of the month, the three-letter abbreviation for the month, and the four-digit year. The parse method of the SimpleDateFormat class is then used to parse the string into a Date object, which can be printed or used for other purposes. If the string doesn't match the expected format, a ParseException is thrown.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment