Quick revision
The short answer
We migrate from the edges inwards: JDBC 4.2 drivers read and write java.time types directly through getObject and setObject, internal code changes first and converts at library boundaries, and public APIs gain java.time overloads while the Date versions are deprecated. The hard part is deciding for each field whether it is an instant, a local date or a zoned value. Static analysis blocks new uses of Date, Calendar and SimpleDateFormat, and tests check that data round-trips correctly around DST and zone boundaries.
What to remember
In this answer, we planned a migration from the edges inwards: JDBC 4.2 getObject and setObject at the data access edge, internal code next with conversions at library boundaries, and public APIs last through new java.time overloads and deprecated Date methods. We saw that the hard part is classifying each field as an instant, a local date or a zoned value, recording the zone its old values were created in, because a wrong choice moves data by hours or a day. A frozen static analysis rule blocks new legacy code, and round-trip tests around DST and zone boundaries prove each field.
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.
We migrate from the edges inwards: JDBC 4.2 drivers read and write java.time types directly through getObject and setObject, internal code changes first and converts at library boundaries, and public APIs gain java.time overloads while the Date versions are deprecated. The hard part is deciding for each field whether it is an instant, a local date or a zoned value. Static analysis blocks new uses of Date, Calendar and SimpleDateFormat, and tests check that data round-trips correctly around DST and zone boundaries.
Why this question is asked
Migrating a large codebase from java.util.Date to java.time is less a refactoring than a data-meaning project. The mechanical part, changing types, is easy. The dangerous part is that each Date field silently encodes a decision someone made years ago, such as 'midnight on the server is a birthday', and a careless conversion moves that birthday by a day. Interviewers ask this to see whether we can plan a migration that many teams can run in parallel without breaking each other. In this answer, we will plan the order of work, classify the fields, migrate the JDBC edge, migrate public library APIs compatibly, block new legacy code, and test the round trips.
1Planning the Order: From the Edges Inwards
Code using Date sits in three kinds of places. The data access edge reads and writes the database. Internal code, the services and domain models, passes values around. Public library APIs are used by other teams, sometimes by code we cannot see. Each needs a different approach, and they cannot all change at once.
The plan works from the edges inwards, keeping every step compatible. First we fix the data access edge so it can produce java.time types. Then each team migrates its internal code, converting at boundaries to libraries that still use Date. Public APIs come last and most slowly, because their callers decide the pace. At every stage, the system still compiles and runs.
- Inventory and classify fieldsinstant, local date, zoned or local date-time
- Block new legacy usagefrozen ArchUnit rule: existing allowed, new rejected
- Data access edgeJDBC 4.2
getObjectandsetObject, JPAjava.timefields - Internal code, team by teamconvert at library boundaries
- Public library APIsadd
java.timeoverloads, deprecate Date ones - Remove deprecated APIsafter callers have moved
6 steps in order. Play them, or step through them one at a time.
2The Hard Part: Deciding What Each Field Means
java.util.Date is always an instant technically, but teams used it for at least three different meanings. Some fields really are instants, like created_at. Some are calendar dates, like a birthday or an invoice date, stored as midnight in some zone. Some are local times in a particular place, like an appointment. java.time has a different type for each, so for every field someone must decide which it was. That decision cannot be automated, which is why it is the hard part.
Getting it wrong moves data. Here is a birthday that an old service created at local midnight on a server in Europe/Berlin, converted carelessly with UTC.
| Field | Real meaning | Target type | Zone of old values |
|---|---|---|---|
Order.createdAt | moment it happened | Instant | n/a (already an instant) |
Customer.birthday | calendar date | LocalDate | Europe/Berlin server midnight |
Invoice.dueDate | calendar date | LocalDate | UTC midnight (batch job) |
Appointment.start | local time in a place | LocalDateTime + ZoneId | zone column exists |
So the inventory records, for each field, its meaning, its target java.time type and the zone in which old values were created. The table below shows the shape of that inventory; in practice it is a spreadsheet or a file in the repository, owned by the team that owns the field.
3Migrating the Data Access Edge
Since JDBC 4.2, which came with Java 8, drivers support java.time types directly through getObject and setObject: LocalDate for DATE, LocalDateTime for a timestamp without time zone, and OffsetDateTime for a timestamp with time zone. That removes the hidden default-zone conversions that getTimestamp and setTimestamp perform. With JPA, Hibernate maps java.time fields natively, so entities can simply change their field types.
Notice that the column types must match the meanings decided in the inventory. If created_at is a timestamp without time zone, the migration is a good moment to change it to timestamp with time zone, with a conversion that names the zone the old values were written in.
4Internal Code and Public APIs Without Breaking Callers
Inside a team's own code, we switch types freely, converting at the boundary with any library that still uses Date: Date.from(instant) going out, date.toInstant() coming in. Those conversion calls are temporary and can be found later by searching for them.
Public library APIs need more care, because other teams call them. We add new overloads or new methods that take and return java.time types, and mark the Date versions @Deprecated with a forRemoval flag and a note pointing to the replacement. The old methods delegate to the new ones, so there is only one implementation to maintain. After the callers have moved, measured with a search across repositories or a usage report, we remove the old methods in a major version.
5Blocking New Legacy Code
A migration that takes a year will be undone by new code unless we stop it. A static analysis rule fails the build when code depends on Date, Calendar or SimpleDateFormat. Because existing code still has thousands of uses, we freeze the rule: ArchUnit's freezing mechanism records today's violations and fails only on new ones, so the number can only go down. The same approach works with Error Prone checks or a Maven plugin that flags legacy APIs.
6Testing the Round Trips
Each migrated field gets tests that write a value and read it back through the real stack: entity, Hibernate, driver and a real database in Testcontainers. The values that matter are the edge cases: instants just before and after a DST change, local times in the spring gap and autumn overlap, dates near midnight in zones far from UTC, such as Pacific/Auckland and America/Los_Angeles, and 29 February. We run them with the JVM default zone set to something other than UTC, because many old bugs only appear there. A comparison job that reads a sample of production rows with the old code and the new code, and reports differences, catches classification mistakes before users do.
7Follow-up questions
Should we convert the stored data or only the Java types?
Both, field by field. Changing the Java type without fixing the column leaves the old default-zone conversions in place. Where a column's type does not match its meaning, such as an instant in a timestamp without time zone, we migrate the column with a conversion that states the zone of the old values, after checking a sample against an independent source.
How do you keep many teams moving in step?
We do not require them to. The edge-inwards order means each team can migrate its own code when it is ready, because conversions at boundaries keep everything compatible. A central dashboard of the frozen rule's violation counts per repository shows progress, and the platform team helps the teams with the most date-sensitive code first.
What about Jackson and other serialisers?
They need the java.time module configured consistently, ideally in a shared library, so a migrated DTO keeps the same wire format. We check with contract tests that the JSON before and after the migration is identical, or that the change is a deliberate, versioned one.
8Conclusion
In this answer, we planned a migration from the edges inwards: JDBC 4.2 getObject and setObject at the data access edge, internal code next with conversions at library boundaries, and public APIs last through new java.time overloads and deprecated Date methods. We saw that the hard part is classifying each field as an instant, a local date or a zoned value, recording the zone its old values were created in, because a wrong choice moves data by hours or a day. A frozen static analysis rule blocks new legacy code, and round-trip tests around DST and zone boundaries prove each field.
Open with
I'd plan it from the edges inwards so every step stays compatible. First an inventory: for every Date field, decide whether it's really an instant, a calendar date, or a local time in a place, and which zone old values were created in.
Then, if they want more
That's the hard part and it can't be automated, because a birthday stored as server midnight in Berlin becomes the previous day if you convert it with UTC. At the same time I'd add a frozen ArchUnit rule that allows existing uses of Date, Calendar and SimpleDateFormat but fails on new ones.
Then the data access edge: JDBC 4.2 drivers support java.time through getObject and setObject, LocalDate for DATE, OffsetDateTime for timestamptz, and Hibernate maps java.time fields natively, which also removes hidden default-zone conversions. Next, teams migrate internal code at their own pace, converting with Date.from and toInstant at boundaries with libraries that still use Date.
Public APIs come last: add overloads that take java.time types, deprecate the Date versions for removal and delegate to the new ones, then remove them in a major version once callers have moved. Every migrated field gets round-trip tests through a real database around DST changes, midnight in far-off zones and leap days, with a non-UTC default zone.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- 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 large codebase still uses java.util.Date and Calendar in domain models, JDBC code and...": https://scrivio-interview-library.netlify.app/java/date-and-time/768ada3883/