Working with Date and Time API
1. Using LocalDate
| Method | Returns |
LocalDate.now() | Today (system zone) |
LocalDate.of(y,m,d) | Specific date |
parse(text) | ISO yyyy-MM-dd |
plusDays/Months/Years(n) | New instance |
getDayOfWeek() | DayOfWeek enum |
isLeapYear() | boolean |
2. Using LocalTime
| Method | Result |
LocalTime.of(h,m,s) | Specific time |
plusHours/Minutes(n) | Wraps at 24h |
truncatedTo(ChronoUnit.MINUTES) | Drop sub-units |
| Constants | MIN, MAX, MIDNIGHT, NOON |
3. Using LocalDateTime
| Aspect | Detail |
| Composition | LocalDate + LocalTime, no zone |
| Use case | Wall-clock time without TZ semantics |
| Combine | date.atTime(time) / time.atDate(date) |
| To zoned | atZone(ZoneId.of("UTC")) |
4. Using ZonedDateTime
| Method | Use |
ZonedDateTime.now(zone) | Now in zone |
withZoneSameInstant(zone) | Convert TZ, keep instant |
withZoneSameLocal(zone) | Keep wall clock, change zone |
toInstant() | UTC instant |
| DST handling | Auto resolves gaps/overlaps |
5. Using OffsetDateTime
| vs ZonedDateTime | Behavior |
| OffsetDateTime | Fixed offset (e.g., +05:30); no DST |
| ZonedDateTime | Zone with DST rules (e.g., America/New_York) |
| Use OffsetDateTime | Logs, DB timestamps, REST APIs |
| ISO format | 2026-05-12T10:00:00+05:30 |
6. Using Instant
| Method | Result |
Instant.now() | UTC nanosecond timestamp |
ofEpochSecond/Milli | From epoch |
plus(Duration) | Arithmetic |
atZone(zone) | To ZonedDateTime |
| Best for | Timestamps, machine time |
7. Using Period and Duration
| Type | Granularity | Example |
Period | Date-based (Y/M/D) | Period.between(d1, d2) |
Duration | Time-based (s/ns) | Duration.ofMinutes(30) |
| Apply | date.plus(period) | or instant.plus(duration) |
| Pattern | Builtin |
| ISO_LOCAL_DATE | 2026-05-12 |
| ISO_OFFSET_DATE_TIME | 2026-05-12T10:00+05:30 |
| ofPattern("dd/MM/yyyy") | Custom |
| ofLocalizedDate(MEDIUM) | Locale-aware |
Note: DateTimeFormatter is immutable and thread-safe — cache as static final.
9. Parsing Dates (parse())
| Method | Use |
LocalDate.parse(s) | ISO default |
LocalDate.parse(s, fmt) | Custom format |
| Failure | Throws DateTimeParseException |
10. Manipulating Dates
| Method | Behavior |
plus / minus | By unit or amount |
with(field, value) | Replace field |
withYear / Month / DayOfMonth | Field setters |
truncatedTo(unit) | Time only |
11. Using TemporalAdjusters
| Adjuster | Result |
firstDayOfMonth() | 1st of month |
lastDayOfMonth() | End of month |
next(MONDAY) | Next weekday |
dayOfWeekInMonth(2, FRIDAY) | 2nd Friday |
| Custom | TemporalAdjusters.ofDateAdjuster(fn) |
Example: Next payday
LocalDate payday = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
12. Handling Time Zones
| API | Use |
ZoneId.systemDefault() | JVM default |
ZoneId.of("America/New_York") | IANA region |
ZoneOffset.ofHours(-5) | Fixed offset |
ZoneId.getAvailableZoneIds() | All zones |
| DST resolver | ZonedDateTime.ofLocal(...) |
13. Converting Legacy Dates
| From → To | Conversion |
| Date → Instant | date.toInstant() |
| Instant → Date | Date.from(instant) |
| Calendar → ZonedDateTime | cal.toInstant().atZone(cal.getTimeZone().toZoneId()) |
| java.sql.Date → LocalDate | sqlDate.toLocalDate() |
| java.sql.Timestamp → LocalDateTime | ts.toLocalDateTime() |
Warning: java.util.Date LEGACY — replace with java.time.* in new code.