java.time API basics
Use immutable, clear date/time types instead of Date and Calendar.
Porównanie kodu
✕ Pre-Java 8
// Mutable, confusing, zero-indexed months Calendar cal = Calendar.getInstance(); cal.set(2025, 0, 15); // January = 0! Date date = cal.getTime(); // not thread-safe
✓ Java 8+
LocalDate date = LocalDate.of(
2025, Month.JANUARY, 15);
LocalTime time = LocalTime.of(14, 30);
Instant now = Instant.now();
// immutable, thread-safe
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
Immutable
Date/time values can't be accidentally modified.
Clear API
Month.JANUARY, not 0. DayOfWeek.MONDAY, not 2.
Thread-safe
No synchronization needed — share freely across threads.
Stare podejście
Date + Calendar
Nowoczesne podejście
java.time.*
Od JDK
8
Poziom trudności
Początkujący
Wsparcie JDK
java.time API basics
Dostępne
Widely available since JDK 8 (March 2014)
Jak to działa
java.time provides LocalDate, LocalTime, LocalDateTime, Instant, ZonedDateTime — all immutable and thread-safe. Months are 1-indexed. No more Calendar.JANUARY = 0 confusion.
Powiązana dokumentacja