How to Parse Date String in Java: Simple Guide with Examples
To parse a date string in Java, use
DateTimeFormatter with LocalDate or LocalDateTime for modern code, or SimpleDateFormat with Date for legacy code. Define the date pattern matching your string, then call parse() to convert the string into a date object.Syntax
Use DateTimeFormatter to define the pattern of your date string. Then use LocalDate.parse() or LocalDateTime.parse() with the formatter to convert the string to a date object.
For legacy code, use SimpleDateFormat with the pattern, then call parse() on the string to get a Date object.
java
import java.time.LocalDate; import java.time.format.DateTimeFormatter; // Define formatter with pattern DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Parse string to LocalDate LocalDate date = LocalDate.parse("2024-06-15", formatter);
Example
This example shows how to parse a date string "2024-06-15" into a LocalDate object using DateTimeFormatter. It then prints the parsed date.
java
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateParseExample { public static void main(String[] args) { String dateString = "2024-06-15"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate date = LocalDate.parse(dateString, formatter); System.out.println("Parsed date: " + date); } }
Output
Parsed date: 2024-06-15
Common Pitfalls
- Using a pattern that does not match the date string causes
DateTimeParseException. - Using
SimpleDateFormatwithout settingsetLenient(false)can lead to unexpected parsing results. - Parsing date-time strings without time parts into
LocalDateTimecauses errors.
java
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; public class PitfallExample { public static void main(String[] args) { String wrongDate = "15-06-2024"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); try { // Wrong pattern for the string format LocalDate date = LocalDate.parse(wrongDate, formatter); } catch (DateTimeParseException e) { System.out.println("Error: " + e.getMessage()); } } }
Output
Error: Text '15-06-2024' could not be parsed at index 0
Quick Reference
Here is a quick summary of common date patterns:
| Pattern | Description | Example |
|---|---|---|
| yyyy-MM-dd | Year-Month-Day | 2024-06-15 |
| dd-MM-yyyy | Day-Month-Year | 15-06-2024 |
| MM/dd/yyyy | Month/Day/Year | 06/15/2024 |
| yyyy-MM-dd HH:mm:ss | Date and 24-hour time | 2024-06-15 13:45:30 |
| yyyy-MM-dd'T'HH:mm:ss | ISO date-time | 2024-06-15T13:45:30 |
Key Takeaways
Use DateTimeFormatter with LocalDate or LocalDateTime to parse date strings in modern Java.
Ensure the pattern matches the date string format exactly to avoid parsing errors.
SimpleDateFormat is legacy; prefer DateTimeFormatter for thread safety and clarity.
Handle exceptions like DateTimeParseException to catch invalid date strings.
Use setLenient(false) with SimpleDateFormat to avoid unexpected parsing results.