Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Which Java type would you use for timestamps in an API: LocalDateTime, OffsetDateTime or Instant? Explain the JSON format.

For timestamps, points in time such as when an order was created, I would use Instant, or OffsetDateTime when the offset the event happened in is itself useful, and never LocalDateTime, which has no zone and is therefore ambiguous. In JSON they are ISO-8601 strings with an offset, for example 2024-05-01T10:30:00Z. Dates without a time, such as a birthday, are LocalDate ("1990-07-14"). We store and transmit in UTC, convert to the user's zone only for display, and document the format in the API spec.

Why this question is asked

Time bugs are some of the most expensive bugs in distributed systems, because they hide until a client in another time zone, a daylight saving change or a server moved to a different region exposes them. Most of them start with a type choice: a LocalDateTime that everyone assumed was in UTC, or a date of birth stored as midnight in some zone that shows up as the previous day in another. Interviewers ask this design question because it tests whether we know what each java.time type actually means. In this answer, we will recap the types, see why LocalDateTime is ambiguous for timestamps, choose the types and the JSON format, and decide where conversion to local time belongs.

1A Quick Recap of the java.time Types

Java's time types fall into two groups. Some describe a point on the global timeline, the same moment everywhere on Earth. Instant is the purest of these: a count of seconds and nanoseconds since 1970-01-01T00:00:00Z, with no zone at all. OffsetDateTime is a date and time plus an offset from UTC, such as 12:30 at +02:00, which also identifies one exact moment. ZonedDateTime adds a region such as Europe/Berlin, whose offset changes with daylight saving.

The other group describes what a calendar or a clock shows, without saying where. LocalDate is a calendar date, LocalTime a clock time, and LocalDateTime both together. 2024-05-01T10:30 as a LocalDateTime means half past ten on 1 May somewhere, which is a different moment in Berlin, New York and Tokyo.

What each type means
TypeJSON exampleUse it for
Instant"2024-05-01T10:30:00Z"timestamps: created, paid, expires
OffsetDateTime"2024-05-01T12:30:00+02:00"timestamps where the local offset matters
ZonedDateTime"2024-11-04T09:00:00+01:00[Europe/Berlin]"future local events, recurring meetings
LocalDate"1990-07-14"birthdays, due dates, holidays
LocalDateTime"2024-05-01T10:30:00"rarely in APIs: no zone, ambiguous
Only the first three identify a single moment; LocalDateTime does not.

2Why LocalDateTime Is Ambiguous for Timestamps

Suppose an order service writes createdAt as a LocalDateTime, taken from LocalDateTime.now() on a server whose default zone is Europe/Berlin. The JSON says 2024-05-01T10:30:00. A mobile client in New York does not know that this was Berlin time. If it assumes UTC, it shows the order as created at 06:30 local time, two hours off; if it assumes local time, it shows 10:30, six hours off. Both are wrong, and nothing in the data allows a correct answer.

Then the service moves to a cloud region where the default zone is UTC, and every new timestamp silently shifts by two hours relative to the old ones. Daylight saving adds another trap: in the autumn, the local hour from 02:00 to 03:00 happens twice, so two different moments produce the same LocalDateTime, and sorting by it puts events in the wrong order.

Java
LocalDateTime local = LocalDateTime.now(); // 2024-05-01T10:30 on a Berlin server
Instant instant = Instant.now(); // 2024-05-01T08:30:00Z, the same moment
// JSON a client receives
// {"createdAt":"2024-05-01T10:30:00"} which zone? the client must guess
// {"createdAt":"2024-05-01T08:30:00Z"} one meaning everywhere

3Choosing the Types and the JSON Format

For every field that records when something happened or will expire, the API uses Instant, written as an ISO-8601 string in UTC with the Z suffix, such as 2024-05-01T08:30:00Z. Clients in every language parse this format, it sorts correctly as text, and it is exactly what OpenAPI calls format: date-time (RFC 3339). OffsetDateTime is a reasonable alternative when the offset at which the event happened is meaningful to the client, for example a delivery scanned at 12:30+02:00 at a depot. Either way, the string always carries an offset.

For dates without a time, the API uses LocalDate with format: date, such as 1990-07-14. Converting a birthday to a timestamp is a famous bug: stored as midnight UTC, 1990-07-14T00:00Z is still 13 July in New York, so a client that converts to local time shows the wrong day. A date is not a moment, and the type should say so.

Java
public record OrderView(
String id,
Instant createdAt, // "2024-05-01T08:30:00Z"
Instant expiresAt, // "2024-05-31T23:59:59Z"
LocalDate deliveryDate, // "2024-05-03" - a calendar day, no time
String customerTimeZone) {} // "America/New_York" - for display, if the client needs it
// With Spring Boot's mapper (dates as strings, not timestamps):
// {"id":"ord-42","createdAt":"2024-05-01T08:30:00Z","expiresAt":"2024-05-31T23:59:59Z",
// "deliveryDate":"2024-05-03","customerTimeZone":"America/New_York"}

Notice one Jackson detail with OffsetDateTime: when reading, Jackson 2 by default adjusts the value to the mapper's time zone, which is UTC, so 12:30+02:00 is read as 10:30Z. The moment is the same, but the original offset is lost. If the offset matters, we disable DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE (check the behaviour in your Jackson version).

4Store and Transmit in UTC, Convert for Display

Inside the system, every timestamp is an Instant from end to end: created with Instant.now() or a Clock, stored in a column that holds a moment, such as PostgreSQL's timestamp with time zone (timestamptz), which stores UTC internally, and sent as UTC strings. Conversion to local time happens at the very last step, in the client or the view layer, using the user's zone: instant.atZone(ZoneId.of("America/New_York")). Formatting for people, such as 1 May 2024, 4:30 AM, is also a display concern and never part of the API.

The exception is a future event defined in local terms, such as a meeting every Monday at 09:00 in Berlin. If we stored it as an Instant, a change in daylight saving rules would move the meeting. Those are stored as a local date and time plus a zone id, and converted to an Instant when they occur.

Step throughWhere time zones belong
  1. Client displayconvert to the user's zone, format for people
  2. JSON APIISO-8601 with offset, UTC: 2024-05-01T08:30:00Z
  3. Service codeInstant and LocalDate; Clock injected for tests
  4. Databasetimestamptz (moments) and date (calendar days)

4 layers, from the top down. Step through them one at a time.

Only the top layer knows about the user's zone.

5Documenting the Format

The choice only helps if clients know it. In the OpenAPI spec, every timestamp is type string with format date-time and an example ending in Z, every calendar date is format date, and the API guidelines state the rule in one sentence: timestamps are UTC instants in RFC 3339 format, dates are calendar days without a zone. A contract test that serializes a sample response and checks the strings keeps the implementation honest.

6Follow-up questions

Is OffsetDateTime or Instant better for an API?

For most timestamps, Instant: it has one canonical form and no question about which offset to use. OffsetDateTime is better when the local offset of the event carries information for the client. Both are correct, because both identify a single moment; the mistake is a type without an offset.

When is LocalDateTime the right type?

When the value really has no zone, such as a local opening time in a store's own settings, or as a step in converting a future local event together with its zone. As a timestamp in an API or a database column for when something happened, it is almost always a bug waiting for a second time zone.

How do we test time-dependent code?

By injecting a java.time.Clock instead of calling Instant.now() directly. Tests use Clock.fixed(Instant.parse("2024-05-01T08:30:00Z"), ZoneOffset.UTC), and production uses Clock.systemUTC(), so tests are deterministic and never depend on the machine's zone.

7Conclusion

In this answer, we saw that Instant and OffsetDateTime identify a moment while LocalDateTime only shows what a clock reads somewhere, which makes it ambiguous for timestamps across zones, server moves and daylight saving changes. The API uses Instant (or OffsetDateTime) as ISO-8601 strings with an offset, such as 2024-05-01T10:30:00Z, and LocalDate for calendar dates like birthdays. Values are stored and transmitted in UTC, converted to the user's zone only for display, future local events keep their zone id, and the formats are documented as date-time and date in the API spec.

What to say in the interviewAbout 90 seconds aloud

Open with

For timestamps, things like created, paid or expires, I'd use Instant, or OffsetDateTime if the offset where the event happened is useful to the client. Both identify an exact moment.

Then, if they want more

I'd avoid LocalDateTime, because it has no zone: 10:30 on 1 May is a different moment in Berlin and New York, so the client has to guess, and it breaks again when the server's default zone changes or daylight saving repeats an hour.

In JSON I'd use ISO-8601 strings with an offset, normally UTC with the Z suffix, like 2024-05-01T10:30:00Z, which every client parses and which sorts correctly as text; that's format date-time in OpenAPI.

For dates without a time, like a birthday or a delivery day, I'd use LocalDate, written as 1990-07-14, because turning a date into midnight UTC makes it show as the previous day in America.

Internally I keep everything in UTC: Instant in code, timestamptz in PostgreSQL, UTC on the wire, and only the client or view converts to the user's zone and formats it for people. The exception is a future event in local terms, like a weekly meeting at nine in Berlin, which I store as local time plus zone id.

And I document it in the API spec and inject a Clock so tests are deterministic.

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.
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 "Which Java type would you use for timestamps in an API: LocalDateTime, OffsetDateTime or...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/4e29579f37/