CKTIS / Java
Parsing, creating, and formatting dates and date-times in Java
Build dates from fields, parse ISO or custom text, and format dates and date-times without assigning a time zone.
Version: Java SE 21
Syntax
Java SE 21 — java.time.LocalDate and java.time.LocalDateTime; formatter type: java.time.format.DateTimeFormatter
Static factory and parsing methods:
LocalDate LocalDate.of(int year, int month, int dayOfMonth)
LocalDate LocalDate.of(int year, Month month, int dayOfMonth)
LocalDate LocalDate.parse(CharSequence text)
LocalDate LocalDate.parse(CharSequence text, DateTimeFormatter formatter)
LocalDateTime LocalDateTime.of(LocalDate date, LocalTime time)
LocalDateTime LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute)
LocalDateTime.of also accepts java.time.Month instead of an int month. Both component-based forms have overloads with a trailing int second or with trailing int second, int nanoOfSecond.
Instance methods:
String date.format(DateTimeFormatter formatter) // date: LocalDate
String date.toString() // date: LocalDate
String dateTime.format(DateTimeFormatter formatter) // dateTime: LocalDateTimeIn java.time, LocalDate represents an ISO date alone, while LocalDateTime combines an ISO date with a local time. Neither stores a time zone or UTC offset, and neither identifies a unique instant by itself. Both types are immutable.
LocalDate.of(year, month, dayOfMonth) is a static factory that accepts a month number from 1 to 12 or a Month constant. The static factory LocalDateTime.of(date, time) combines a LocalDate and a LocalTime. The field-based overloads accept a month number or Month, require an hour and minute, and let you add a second and then a nanosecond. An omitted second or nanosecond is zero. Invalid field values, such as February 29 in a non-leap year, cause a DateTimeException.
The static method LocalDate.parse(text) uses DateTimeFormatter.ISO_LOCAL_DATE, for example with 2024-02-29. The overload LocalDate.parse(text, formatter) uses the supplied formatter, for example with the pattern dd/MM/uuuu. Text that cannot be parsed into a date causes a DateTimeParseException. DateTimeFormatter.ofPattern defaults to SMART, which can adjust 29/02/2023 to February 28. To reject such dates, set ResolverStyle.STRICT and use uuuu (year), not yyyy (year of era).
The instance methods LocalDate.format(formatter) and LocalDateTime.format(formatter) return a String using the supplied formatter without changing the value. If a required field is unavailable, such as the hour for HH:mm on a LocalDate, formatting throws DateTimeException. LocalDate.toString() takes no formatter and returns ISO-8601 text such as 2024-02-29. Patterns are case-sensitive: MM means month, mm means minute, and HH means hour on a 24-hour clock.
Examples
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dateFormat = DateTimeFormatter
.ofPattern("dd/MM/uuuu", Locale.ROOT)
.withResolverStyle(ResolverStyle.STRICT);
DateTimeFormatter dateTimeFormat = DateTimeFormatter
.ofPattern("dd/MM/uuuu HH:mm", Locale.ROOT);
LocalDate date = LocalDate.of(2024, 2, 29);
LocalDate fromIso = LocalDate.parse("2024-02-29");
LocalDate fromCustom = LocalDate.parse("29/02/2024", dateFormat);
System.out.println(date.equals(fromIso) && date.equals(fromCustom));
System.out.println(date.toString());
System.out.println(date.format(dateFormat));
LocalDateTime dateTime = LocalDateTime.of(date, LocalTime.of(9, 5));
System.out.println(dateTime.format(dateTimeFormat));
try {
LocalDate.parse("29/02/2023", dateFormat);
System.out.println("Unexpected success");
} catch (DateTimeParseException e) {
System.out.println("Invalid date rejected");
}
}
}Result
true 2024-02-29 29/02/2024 29/02/2024 09:05 Invalid date rejected
The first line confirms that both parsed values equal the date from the factory. The next three lines show ISO date text, custom date text, and date-time text. The last line shows that strict parsing rejects February 29, 2023.
Related lessons
- Java · Lesson 61 · Advanced · Version: 2