Quick revision
The short answer
LocalDateTime.now() reads the clock in the JVM's default time zone, so servers in two regions store wall-clock readings from two different zones, and the column keeps no record of which zone each value came from. Reports read every value as if it were in the first region's zone, so orders from a region ahead of it look hours in the future. The fix is to record event times as instants, with Instant.now() or OffsetDateTime in UTC, store them in a timestamp with time zone column or in UTC by documented convention, and check the JDBC driver and database session zone settings too.
What to remember
In this answer, we saw that LocalDateTime.now() reads the clock in the JVM default zone, so two regions with different defaults stored readings from two zones into a column that keeps no zone, and reports misread them. We fixed it by recording moments with Instant.now() or OffsetDateTime in UTC, storing them in timestamptz or in UTC by documented convention, setting the driver, Hibernate and JVM zones explicitly, and repairing the old rows with the zone each was actually written in. A test with a non-UTC default zone keeps it fixed.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
LocalDateTime.now() reads the clock in the JVM's default time zone, so servers in two regions store wall-clock readings from two different zones, and the column keeps no record of which zone each value came from. Reports read every value as if it were in the first region's zone, so orders from a region ahead of it look hours in the future. The fix is to record event times as instants, with Instant.now() or OffsetDateTime in UTC, store them in a timestamp with time zone column or in UTC by documented convention, and check the JDBC driver and database session zone settings too.
Why this question is asked
This bug is almost a rite of passage. A service works for years in one region, everybody's servers share a zone, and then a second region goes live and the reports start lying. Interviewers like it because it tests whether we know that a LocalDateTime is not a moment, and whether we think beyond the Java code to the driver and the database. In this answer, we will recap what LocalDateTime.now() actually does, reproduce the bug with two servers, see why the stored data cannot be trusted, and then fix the code, the column and the connection settings, and repair the data already written.
1A Quick Recap of LocalDateTime.now()
LocalDateTime is a date and a clock time with no zone. It cannot be a moment on its own, because 10:00 in Frankfurt and 10:00 in Virginia are six hours apart. When we call LocalDateTime.now() without arguments, Java has to pick a zone to read the clock in, and it picks the JVM's default zone. That default comes from the -Duser.timezone flag, the TZ environment variable, or the operating system's setting, in that rough order.
So the call is really LocalDateTime.now(ZoneId.systemDefault()). The result depends on how the machine is configured, not on anything in our code. As long as every server shared one zone, nobody noticed.
2Reproducing the Bug With Two Servers
Suppose region A runs with the default zone America/New_York and region B, the new one, runs with Europe/Berlin. Two customers place orders at exactly the same moment, 14:00 UTC.
5 messages in order. Play them, or step through them one at a time.
Both orders were placed at 14:00 UTC. The column only kept the clock readings.
Notice that both orders happened at the same moment, yet the stored values are six hours apart. Nothing failed, no exception was thrown. The code did exactly what we wrote; it just wrote the wrong thing.
3Why the Stored Data Cannot Be Trusted
The deeper problem is that the column type, timestamp without time zone, stores only the date and the clock reading. It has nowhere to record which zone the reading came from. So once the value is written, any reader has to guess the zone. The report guessed New York, which is right for region A's rows and wrong for region B's.
There is a second, quieter catch. Even in one region, a LocalDateTime is ambiguous once a year: when New York's clocks go back in November, 01:30 happens twice, and a LocalDateTime cannot say which of the two it means. So the old design was fragile before region B existed; the second region only made the fault visible.
4Fix 1: Record Moments, Not Clock Readings
An order time is a moment, so we record it as one. Instant.now() reads the current point on the timeline and does not depend on the default zone. If we prefer a type that carries its offset, OffsetDateTime.now(ZoneOffset.UTC) works as well.
Now both regions write the same value for the same moment. When the report needs a local view, for example 'orders per day in Berlin', it converts explicitly with atZone(ZoneId.of("Europe/Berlin")) at the edge. Injecting a Clock rather than calling Instant.now() directly also makes the code testable.
5Fix 2: Use a Column and Connection That Keep the Moment
In PostgreSQL we change the column to timestamp with time zone, usually called timestamptz. Despite its name, it does not store a zone; it stores a moment normalised to UTC and converts to the session's zone only when displaying. In MySQL, the closest equivalent is TIMESTAMP, which also converts through the session zone, or a DATETIME that we fill with UTC by a documented rule.
Then we check the layers in between. The JDBC driver and the database session both have a time zone setting, and conversions can happen there. The PostgreSQL JDBC driver, for instance, sets the session zone from the JVM's default zone when it connects. MySQL Connector/J has its own connection time zone property (check your driver version for the exact name), and Hibernate has hibernate.jdbc.time_zone. We set these explicitly, typically to UTC, and run every JVM with -Duser.timezone=UTC so a forgotten conversion behaves the same everywhere.
Notice the USING clause. It tells PostgreSQL which zone the old readings were in. That is only true for region A's rows, which leads to the last step.
6Repairing the Rows Already Written
Rows written by region B since the launch were Berlin readings. If we have a column that says which region wrote each row, or the server name in the logs, we convert those rows with Europe/Berlin instead. If nothing records it, we have to reconstruct it from other evidence, such as payment provider timestamps, which usually are in UTC. This is the real cost of the bug: data written without a zone cannot be fixed by code alone.
Finally, we add a test that runs the write path with a non-UTC default zone, for example by starting the test JVM with -Duser.timezone=Asia/Tokyo, and asserts that the stored value equals the Instant we expected. That keeps the bug from coming back when someone adds a new timestamp field.
7Follow-up questions
Would setting every server to UTC have been enough?
It would stop the regions from disagreeing, so it is a sensible safety net. But the code would still store a zone-less value and still depend on configuration it does not control. A new deployment, a developer laptop or a batch job with a different default would bring the bug back. Recording an Instant makes the code correct no matter how the server is set up.
Does timestamptz in PostgreSQL store the time zone?
No. It stores a moment in UTC. On input, a value with an offset is converted to UTC; a value without an offset is interpreted in the session's TimeZone setting. On output, it is shown in the session's zone. So the original offset is not kept; if we need it, for example to show a customer their own local time, we store the zone ID in a separate column.
Is LocalDateTime ever right for a database column?
Yes, for values that are deliberately not moments, such as a future appointment's wall-clock time stored next to its zone ID, or a shop's opening time. The rule is that LocalDateTime never represents when something happened.
8Conclusion
In this answer, we saw that LocalDateTime.now() reads the clock in the JVM default zone, so two regions with different defaults stored readings from two zones into a column that keeps no zone, and reports misread them. We fixed it by recording moments with Instant.now() or OffsetDateTime in UTC, storing them in timestamptz or in UTC by documented convention, setting the driver, Hibernate and JVM zones explicitly, and repairing the old rows with the zone each was actually written in. A test with a non-UTC default zone keeps it fixed.
Open with
The problem is that LocalDateTime.now() reads the clock in the JVM's default time zone. In the first region every server had the same default, so nobody noticed. The new region's servers have a different default, so for the same moment they store a different clock reading, for example 09:00 from New York and 15:00 from Berlin.
Then, if they want more
The column is a timestamp without time zone, so it keeps only the reading, not where it came from, and the report interprets everything as the original region's time. That makes region B's orders look hours in the future. The fix is to treat order time as a moment: use Instant.now, ideally from an injected Clock, or OffsetDateTime in UTC.
In PostgreSQL I would migrate the column to timestamptz, which stores a moment normalised to UTC, or, on a database without that, store UTC by a documented convention. I would also set the JDBC driver, Hibernate's jdbc time_zone and the JVM to UTC explicitly, so no silent conversion depends on the server.
Then I would repair the existing rows by converting each with the zone of the region that wrote it, and add a test that runs with a non-UTC default zone.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A service stores LocalDateTime.now() in a database column for order times. After...": https://scrivio-interview-library.netlify.app/java/date-and-time/5e1807e652/