To convert a String to a Date in Java, you typically use the SimpleDateFormat class (for versions prior to Java 8) or the DateTimeFormatter class (from Java 8 onward, for LocalDate, LocalDateTime, and ZonedDateTime).
1. SimpleDateFormat (for Java 7 and earlier)
SimpleDateFormat is a subclass of DateFormat, and it’s used to parse and format Date objects in Java. It allows you to define a pattern to match the date format in the string.
Example:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
public class StringToDateExample {
public static void main(String[] args) {
String dateString = "2024-01-01"; // Example date string
// Define the date format that matches the string
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
// Convert the string to a Date object
Date date = dateFormat.parse(dateString);
System.out.println("Converted Date: " + date);
} catch (ParseException e) {
// Handle the case where the string doesn't match the format
System.out.println("Invalid date format.");
}
}
}
Output: Converted Date: Mon Jan 08 00:00:00 GMT 2024
Explanation:
1. SimpleDateFormat is used to define the pattern “yyyy-MM-dd”, which corresponds to a year-month-day format.
2. The parse() method of SimpleDateFormat converts the string into a Date object.
3. If the string doesn’t match the format, a ParseException will be thrown, so it’s important to catch that exception.
2. DateTimeFormatter (Java 8 and later)
Starting from Java 8, the java.time package introduced the DateTimeFormatter class, which is the recommended way to handle date-time conversions. It’s more modern and less error-prone than SimpleDateFormat.
Example:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToDateExample {
public static void main(String[] args) {
String dateString = "2024-01-01"; // Example date string
// Define the date format that matches the string
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// Convert the string to a LocalDate object
LocalDate date = LocalDate.parse(dateString, dateFormatter);
System.out.println("Converted Date: " + date);
}
}
Output: Converted Date: 2024-01-01
Explanation:
1. DateTimeFormatter.ofPattern(“yyyy-MM-dd”) defines the format that matches the string.
2. LocalDate.parse(dateString, dateFormatter) parses the string into a LocalDate object.
3. Unlike SimpleDateFormat, DateTimeFormatter is immutable and thread-safe, making it a better choice for modern Java code.
3. Converting to LocalDateTime (if time is included)
If the input string includes both date and time (e.g., “2024-12-08 15:30”), you can convert it to a LocalDateTime.
Example:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class StringToDateTimeExample {
public static void main(String[] args) {
String dateTimeString = "2024-01-01 10:15"; // Example date and time string
// Define the format that matches the string
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
// Convert the string to a LocalDateTime object
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, dateTimeFormatter);
System.out.println("Converted DateTime: " + dateTime);
}
}
Output: Converted DateTime: 2024-01-01T10:15