Date and time API
java.time is the modern date and time API. It uses immutable types: Instant for a moment on the timeline, LocalDate/LocalDateTime for civil dates, and ZonedDateTime when a time zone matters.
Detailed Explanation
java.util.Date is a mutable instant with a misleading name. Calendar is mutable and hard to use. Java 8 replaced both with java.time, designed after Joda-Time. Every type is immutable. Methods named plus, minus, and with return new values. The old object does not change.
Pick the type from the question you are answering.
Instantis a point on the UTC timeline (nanoseconds since the epoch). Use it for logs, deadlines, and anything you store as "when it happened".LocalDateis a calendar date without a time or zone (2026-09-12). Use it for birthdays and business dates.LocalTimeis a clock time without a date.LocalDateTimeis a date and time without a zone. It does not name a unique instant (11:00 in Mumbai and 11:00 in Paris are different instants).ZonedDateTimeis a local date-time plus aZoneId(Asia/Kolkata). Use it for "a meeting at 10:00 in this city".OffsetDateTimecarries a fixed offset (+05:30) without zone rules.Durationis an amount of time in seconds and nanos.Periodis an amount in years, months, and days.
Conversion needs a zone: localDate.atStartOfDay(zone).toInstant(). Parsing and formatting use DateTimeFormatter. Do not use SimpleDateFormat; it is mutable and not thread-safe. now() without a clock is hard to test; inject a Clock.
You reach for java.time in all new code. You convert Date at the boundary with date.toInstant() and Date.from(instant). Databases map Instant to timestamps and LocalDate to dates; check whether the column is TIMESTAMP WITH TIME ZONE.
The cost of LocalDateTime stored as "the timestamp" is ambiguity around DST and around servers in different zones. Store Instant (or UTC) for events. Store LocalDate for civil dates. Store a ZoneId next to a local time when the civil time is the business rule. Never do calendar arithmetic on epoch millis by hand.
Code Examples
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class When {
public static void main(String[] args) {
Instant now = Instant.parse("2026-09-12T00:00:00Z");
ZonedDateTime india = now.atZone(ZoneId.of("Asia/Kolkata"));
LocalDate date = india.toLocalDate();
System.out.println(india); // prints: 2026-09-12T05:30+05:30[Asia/Kolkata]
System.out.println(date); // prints: 2026-09-12
}
}import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Parse {
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd-MM-uuuu");
LocalDate d = LocalDate.parse("12-09-2026", fmt);
System.out.println(d.plusDays(1)); // prints: 2026-09-13
}
}Common Gotchas
`LocalDateTime` is not an instant
it has no zone. Two servers can interpret it differently. Store `Instant` for events.
`DateTimeFormatter` patterns are not `SimpleDateFormat` patterns
`Y` is week-based year; use `u` or `y` for calendar year with care.
`Clock` is how tests freeze time
`Instant.now()` in production code cannot be controlled without a clock argument or a static mock.
`Date` and `java.time` meet at `Instant`
convert at the boundary; do not mix `Calendar` arithmetic with `ZonedDateTime`.