Skip to content
Ameen Abozaid
BEG-033

Date and time

  • 5 Lessons
  • 54 min of reading
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • LocalDate
  • LocalDateTime
  • Instant
  • Duration
  • ZonedDateTime
  • time zones

Learn first

54 min of reading · 2 h of practice (estimate)

Learning outcomes

  1. Choose between LocalDate, LocalDateTime, Instant, and ZonedDateTime for a requirement.
  2. Do date arithmetic with Duration and Period.
  3. Parse and format dates with DateTimeFormatter.
  4. Explain why timestamps should be stored in UTC and converted for display.

Lessons

  1. 01Why time is hard11 min read
  2. 02Local dates and times11 min read
  3. 03Instants and time zones10 min read
  4. 04Durations, periods, formatting11 min read
  5. 05Demo: a meeting across time zones and a daylight-saving change11 min read

Lab and exercises

Lab

BEG-033-LAB1 · Meeting planner

Outcomes: 1–4 · Time: ~1.5 h Schedule a meeting in one time zone and show it in three others. Compute days until a deadline. Store the meeting as an Instant and explain in a comment why. Acceptance: correct across a daylight-saving change.

Exercises (assessment: three exercises)

IDOutcomeTask
BEG-033-EX011Choose the type for each: a birthday; a log timestamp; a flight departure with its city's zone; "9 am every day" alarm.
BEG-033-EX022, 3Compute the days between today and 31 December, and the hours between 08:30 and 17:45. Then print a date as 15 Mar 2026.
BEG-033-EX034Write two sentences on why a database should store UTC timestamps and convert at display time.
Show solutions
  • EX01 LocalDate; Instant; ZonedDateTime; LocalTime.
  • EX02 ChronoUnit.DAYS.between(LocalDate.now(), LocalDate.of(year, 12, 31)); Duration.between(LocalTime.of(8,30), LocalTime.of(17,45)).toHours() gives 9 (hours part; toMinutes() gives 555); format with ofPattern("dd MMM yyyy").
  • EX03 UTC has no daylight saving and is one fixed reference, so sorting and comparing are correct and data from many zones agrees. Each user then sees their own local time, converted when displayed.
  • Outcome 2 is also covered by the lab and quiz Q05–Q06.

Quiz

Outcomes: O1 choosing types · O2 Duration and Period · O3 formatting and parsing · O4 UTC storage.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Best type for a birthday:O1
  2. 2Best type for a database "created at" timestamp:O1
  3. 3Which type is NOT a specific moment on the global timeline?O1
  4. 4An event in several time zones should use:O1
  5. 5For hours and minutes use:O2
  6. 6For years, months, days use:O2
  7. 7In DateTimeFormatter patterns, MM means:O3
  8. 8LocalDate.plusDays(5):O3
  9. 9Why store timestamps in UTC?O4
  10. 10Which should you avoid in new code?O1

Practical part

Three exercises BEG-033-EX01 to EX03 plus the lab. Pass: all three exercises correct and the lab correct across a daylight-saving change.

Common mistakes

LocalDateTime for events in multiple time zones; legacy Date.

Assessment

3 exercises.

Career relevance

Timestamps in databases (JNR-028) and distributed clocks (MID-023).

Type to search levels, phases, modules and lessons.