Date and Time API Java 8
Date and Time API
Modern replacement for legacy Date/Calendar classes.
Date Time Examples
import java.time.*;
// Current date/time
LocalDate today = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
// Formatting
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formatted = today.format(formatter);
// Parsing
LocalDate parsed = LocalDate.parse("2023-12-25");Key Points
- Immutable, thread-safe classes.
- LocalDate, LocalTime, LocalDateTime, ZonedDateTime.
- Period/Duration for calculations.
- DateTimeFormatter for formatting/parsing.