Working with Date and Time API

1. Using LocalDate

MethodReturns
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

MethodResult
LocalTime.of(h,m,s)Specific time
plusHours/Minutes(n)Wraps at 24h
truncatedTo(ChronoUnit.MINUTES)Drop sub-units
ConstantsMIN, MAX, MIDNIGHT, NOON

3. Using LocalDateTime

AspectDetail
CompositionLocalDate + LocalTime, no zone
Use caseWall-clock time without TZ semantics
Combinedate.atTime(time) / time.atDate(date)
To zonedatZone(ZoneId.of("UTC"))

4. Using ZonedDateTime

MethodUse
ZonedDateTime.now(zone)Now in zone
withZoneSameInstant(zone)Convert TZ, keep instant
withZoneSameLocal(zone)Keep wall clock, change zone
toInstant()UTC instant
DST handlingAuto resolves gaps/overlaps

5. Using OffsetDateTime

vs ZonedDateTimeBehavior
OffsetDateTimeFixed offset (e.g., +05:30); no DST
ZonedDateTimeZone with DST rules (e.g., America/New_York)
Use OffsetDateTimeLogs, DB timestamps, REST APIs
ISO format2026-05-12T10:00:00+05:30

6. Using Instant

MethodResult
Instant.now()UTC nanosecond timestamp
ofEpochSecond/MilliFrom epoch
plus(Duration)Arithmetic
atZone(zone)To ZonedDateTime
Best forTimestamps, machine time

7. Using Period and Duration

TypeGranularityExample
PeriodDate-based (Y/M/D)Period.between(d1, d2)
DurationTime-based (s/ns)Duration.ofMinutes(30)
Applydate.plus(period)or instant.plus(duration)

8. Formatting Dates (DateTimeFormatter)

PatternBuiltin
ISO_LOCAL_DATE2026-05-12
ISO_OFFSET_DATE_TIME2026-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())

MethodUse
LocalDate.parse(s)ISO default
LocalDate.parse(s, fmt)Custom format
FailureThrows DateTimeParseException

10. Manipulating Dates

MethodBehavior
plus / minusBy unit or amount
with(field, value)Replace field
withYear / Month / DayOfMonthField setters
truncatedTo(unit)Time only

11. Using TemporalAdjusters

AdjusterResult
firstDayOfMonth()1st of month
lastDayOfMonth()End of month
next(MONDAY)Next weekday
dayOfWeekInMonth(2, FRIDAY)2nd Friday
CustomTemporalAdjusters.ofDateAdjuster(fn)

Example: Next payday

LocalDate payday = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());

12. Handling Time Zones

APIUse
ZoneId.systemDefault()JVM default
ZoneId.of("America/New_York")IANA region
ZoneOffset.ofHours(-5)Fixed offset
ZoneId.getAvailableZoneIds()All zones
DST resolverZonedDateTime.ofLocal(...)

13. Converting Legacy Dates

From → ToConversion
Date → Instantdate.toInstant()
Instant → DateDate.from(instant)
Calendar → ZonedDateTimecal.toInstant().atZone(cal.getTimeZone().toZoneId())
java.sql.Date → LocalDatesqlDate.toLocalDate()
java.sql.Timestamp → LocalDateTimets.toLocalDateTime()
Warning: java.util.Date LEGACY — replace with java.time.* in new code.