Java traps · Chapter 15 of 16

LocalDateTime versus Instant

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Money
  • 3 tasks
  • Java
  • In the all labs plan

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

Why you need this

The JVM has a default time zone, the machine's. The server runs in UTC; a laptop in India runs in IST. Code that converts with ZoneId.systemDefault() gives a different date in each place (java -Duser.timezone=... Server.java, which prints the default zone and LocalDate.ofInstant of the same arrival):

UTC: 2026-10-23
Asia/Kolkata: 2026-10-24

Name the zone the business lives in, ZoneId.of("Asia/Kolkata"), and the answer is the same everywhere.

What you build: 3 tasks

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

  1. Task 1

    Repro.java: a sale after midnight that counts in yesterday

    Write reproduce() in Repro.java. Take the time the payment gateway sent for a chai paid at 12:30 am in India on 24 October, "2026-10-23T19:00:00Z" (UTC), parse it with OffsetDateTime.parse, and return Sales.businessDay of it as a String (its toString()).

    The check runs your reproduce() twice, with the default time zone set to UTC as on the server: beside the original Sales.java it must return "2026-10-23" (yesterday's report), and beside a fixed Sales.java it must return "2026-10-24". So it must really ask businessDay about that time. Never call now(). No real waiting and no reading input: reproduce() must finish within 3 seconds.

  2. Task 2

    Sales.java: sales count on India's day

    Fix businessDay(OffsetDateTime paidAt) and isOpen(OffsetDateTime now) in Sales.java so that they go by the clock in India (the zone Asia/Kolkata), whatever offset the time comes with and wherever the server runs: 2026-10-23T19:00:00Z is the 24th (12:30 am in India), 2026-10-23T18:29:59Z is the 23rd, 2026-10-23T20:00+05:30 is the 23rd, and isOpen is true from 6:00 am (open) to 11:00 pm (closed) India time.

    Keep record and its log line, bill <id> ₹<amount> paid <time>, counted on <day>, as they are. The check sends its own times in UTC, in +05:30 and in other offsets, tests either side of midnight and of opening and closing, and runs everything with the default time zone set to UTC and then to Asia/Kolkata: the answers must be the same. Adding 5 hours 30 to the time is right only for times in UTC.

  3. Task 3

    SalesTest.java: a test that would have caught it

    Write a JUnit test class, SalesTest.java, that would have caught this bug before it reached the reports. The check runs your file three times: beside the original Sales.java, where at least one test must fail; beside the fixed Sales.java, where every test must pass; and beside a wrong fix that adds 5 hours 30 to every time as if it were in UTC, where at least one test must fail.

    So test both kinds of input: a UTC time just after midnight in India counts on India's day, and a time that already comes in +05:30, late in the evening, stays on its own day. Never call now(). No real waiting: each test must finish within 3 seconds.

What you will learn

  • Repro.java: a sale after midnight that counts in yesterday
  • Sales.java: sales count on India's day
  • SalesTest.java: a test that would have caught it

More in Money

Build "LocalDateTime versus Instant" yourself

Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

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