Core Java · Chapter 36 of 48

The Date-Time API

By the end of this chapter you can pick the right java.time type, measure time across midnight, show the time in India from a UTC server, and parse and format dates and times. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Core Java
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Ravi is building the SBC shift roster. The night shift, 22:00 to 06:00, shows as minus sixteen hours long, and a receipt printed at a quarter past six in the morning says 00:45. Anjali wants each person's time with Tapri on the staff page, and the time to the next train on the platform screen.

With what you know so far, you would keep minutes since midnight in an int and add 330 for India by hand. The night shift goes negative, the date is wrong for the first five and a half hours of every day, and a test passes at noon and fails at midnight.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Shift.java: how long a shift is, even across midnight

    In Shift.java, make length() return how long the shift is as a Duration, from start to end. A shift whose end is not after its start crosses midnight (crossesMidnight() is already written), and it ends the next day, so its length is never negative.

    For example, 06:00 to 14:00 is PT8H; 22:00 to 06:00 is also PT8H, not PT-16H; 14:00 to 23:30 is PT9H30M; 23:15 to 00:45 is PT1H30M.

  2. Task 2

    Shift.java: is a time inside a day shift or a night shift

    In Shift.java, make covers(t) return true when someone on this shift is working at time t. The start counts and the end does not. For a shift that crosses midnight, a time late in the evening or early in the morning both count.

    For example, 06:00 to 14:00 covers 06:00 and 09:30 but not 14:00 or 03:00; 22:00 to 06:00 covers 22:00, 23:30 and 02:00 but not 06:00 or 12:00.

  3. Task 3

    CounterClock.java: stamp a receipt with the time in India

    In CounterClock.java, make receiptStamp() return the clock's current instant shown in India (the INDIA zone, Asia/Kolkata), whatever zone the clock itself is in, as text: the day with two digits, a space, the month's three-letter English name, a space, the four-digit year, a comma and a space, then the 24-hour time as hours and minutes with two digits each.

    For example, a clock fixed at 2026-10-04T00:35:00Z gives "04 Oct 2026, 06:05"; at 2026-12-31T18:45:00Z it is already the new year in India: "01 Jan 2027, 00:15".

  4. Task 4

    CounterClock.java: how long someone has worked at Tapri

    In CounterClock.java, make tenure(joined) return a Period of years, months and days from the joining date to today's date in India, read from the clock. Joining today gives a Period of zero.

    For example, at 2026-10-04T06:00:00Z, joined 2024-07-15 gives P2Y2M19D and joined 2026-10-04 gives P0D. At 2026-10-03T20:00:00Z it is already 4 October in India, so joined 2026-10-03 gives P1D.

  5. Task 5

    CounterClock.java: how long until the train leaves

    In CounterClock.java, make untilDeparture(departure) read a departure written as day-month-year and 24-hour time, like "04-10-2026 06:50", and return the Duration from now in India (now(), from the clock) until then. It is negative when the train has already left. Text in another format throws a DateTimeParseException.

    For example, with the clock at 06:05 on 4 October 2026 in India, "04-10-2026 06:50" gives PT45M, "05-10-2026 00:10" gives PT18H5M, "04-10-2026 05:45" gives PT-20M, and "2026-10-04 06:50" throws.

What you will learn

  • The types: LocalDate, LocalTime, LocalDateTime, Instant and ZonedDateTime, and which one says what you mean.
  • Duration and Period: an exact amount of time, and calendar years, months and days.
  • Formatting and parsing with DateTimeFormatter patterns.
  • A Clock you pass in, so "now" can be fixed in a test.

More in Core Java

Build The Date-Time API yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.