Memra

Instant, Duration, Period & ZonedDateTime

◈ 4 cards

Machine timestamps, time-based gaps, date-based gaps, and timezone-aware datetimes.

Four more time types

Instant represents a single point on the UTC timeline — a machine timestamp:

Instant now  = Instant.now();
Instant past = Instant.ofEpochSecond(0);  // 1970-01-01T00:00:00Z

Duration measures an amount of time in hours, minutes, seconds, nanoseconds — precise and time-based:

Duration d = Duration.between(start, end);  // between two Instants/LocalTimes
long secs  = d.getSeconds();

Period measures an amount of time in years, months, days — calendar-based:

Period p    = Period.between(birthday, today);  // between two LocalDates
int years   = p.getYears();
int months  = p.getMonths();

ZonedDateTime is a LocalDateTime plus a ZoneId — use it whenever you need to deal with different time zones:

ZoneId       london = ZoneId.of("Europe/London");
ZonedDateTime zdt   = ZonedDateTime.now(london);
ZonedDateTime converted = zdt.withZoneSameInstant(ZoneId.of("America/New_York"));

The ZoneId.of(...) string is an IANA time zone name — always use named zones, never offsets like "+01:00", unless you genuinely mean a fixed offset.

callgives youPeriod.between(birthday, today)years, months, daysDuration.between(start, end) on Instantsseconds and nanosDuration.between(two LocalDates)UnsupportedTemporalTypeExceptionPeriod is date-based; Duration is time-based.
Period is calendar-based and Duration is time-based, so the argument type decides which one is legal. A LocalDate has no time field for Duration to measure — the third row type-checks and then throws at runtime, which is exactly how the exam poses it.
NORMAL ~/memra/learn/java-from-zero/instant-duration-period-zone utf-8 LF