Java Parsing Dates
Parsing turns date text into a date value. A DateTimeFormatter with a pattern tells the parser the shape of the text.
Parse day-first text
The pattern reads 15/01/2026 as day 15, month 1, year 2026. The parsed date prints in ISO order.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("15/01/2026", formatter);
System.out.println(date);
}
}Print back through the same pattern
Formatting runs the conversion the other way. The same formatter turns the date back into day-first text.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("15/01/2026", formatter);
System.out.println(date.format(formatter));
}
}Wrong-shape text is caught
The text 2026-01-15 does not match the day-first pattern, so the parse fails. The catch block prints a plain message.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try {
LocalDate date = LocalDate.parse("2026-01-15", formatter);
System.out.println(date);
} catch (DateTimeParseException e) {
System.out.println("That text is not in day/month/year order.");
}
}
}A date-time taste
The pattern gains an hour and a minute after the date parts. The parsed value prints with a T between date and time.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime moment = LocalDateTime.parse("15/01/2026 10:30", formatter);
System.out.println(moment);
}
}Example
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("03/04/2026", formatter);
System.out.println(date);
System.out.println(date.format(formatter));
System.out.println(date.plusDays(7).format(formatter));
}
}Key points
- LocalDate.parse reads date text with a DateTimeFormatter.
- The pattern dd/MM/yyyy means two-digit day, two-digit month, and four-digit year.
- The format method prints a date back through the same pattern.
- Text in the wrong shape throws DateTimeParseException.
- A catch block for DateTimeParseException can print a plain message of the programโs own.
- A date-time pattern adds hours and minutes after the date parts.
In this example, the text 03/04/2026 parses to April 3, 2026, and formats back to the same day-first text. Adding seven days and formatting again gives April 10, 2026.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which pattern reads a day-first date like 15/01/2026?