Quick revision
The short answer
Every service obtains time from an injected java.time.Clock, and direct now() calls without a clock are banned in production code. A shared starter provides that Clock bean, which in test environments can be offset or fixed through configuration and is guarded so it can never be enabled in production. Because databases, downstream services and schedulers have their own clocks, the standard defines which components shift together and requires scheduler triggers to use the same clock, or jobs will still run at real time.
What to remember
In this answer, we designed time-shifted testing around one rule, that all production code gets time from an injected Clock, enforced by lint rules. A shared starter provides the Clock bean, offset or fixed through configuration in test tiers, with a fail-closed guard that refuses shifting in production. The standard defines which components shift together, with services writing their own timestamps instead of database defaults and stubs for external systems, and wires the same clock into schedulers so jobs fire at shifted times. A staged rollout with a checklist brings every team along.
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.
Every service obtains time from an injected java.time.Clock, and direct now() calls without a clock are banned in production code. A shared starter provides that Clock bean, which in test environments can be offset or fixed through configuration and is guarded so it can never be enabled in production. Because databases, downstream services and schedulers have their own clocks, the standard defines which components shift together and requires scheduler triggers to use the same clock, or jobs will still run at real time.
Why this question is asked
End-of-month billing, annual renewals and year-end reports are some of the riskiest code we have, and they run on only a few days a year. Waiting for the 31st to find a bug in staging is not a strategy. Time-shifted testing lets a staging environment believe it is 31 January today. Interviewers ask this at a senior level because making it work for one service is easy, and making it work across many services, databases and teams is a real design problem. In this answer, we will set the core rule, design the shared starter, guard production, decide which components shift together, handle schedulers, and roll the standard out.
1The Core Rule: One Source of Time
Time shifting only works if every piece of code asks the same place what time it is. So the foundation is a rule: production code obtains the current time from an injected java.time.Clock, and calls such as Instant.now(), LocalDate.now() or System.currentTimeMillis() without a clock are banned outside configuration code. ArchUnit or Error Prone checks in each service's build enforce it.
The reason this comes first is that a single forgotten Instant.now() makes the shifted test unreliable in a quiet way: most of the code thinks it is 31 January, one class thinks it is 10 March, and the results look almost right.
2The Shared Starter
The platform team publishes a Spring Boot starter that every service includes. It defines the Clock bean, reading its mode from configuration. In normal operation it is Clock.systemUTC(). In a test environment, configuration can ask for an offset clock, real time shifted by a fixed amount so time still passes, or a fixed clock for fully deterministic runs.
Notice that the offset is computed by the test pipeline from a target instant, such as 2026-01-31T23:00:00Z, so testers think in dates rather than durations. The starter also logs the active mode at startup and exposes it on an actuator endpoint, so anyone looking at a shifted environment can see immediately that it is shifted.
3Guarding Production
A shifted clock in production would be a disaster: invoices dated next month, tokens that never expire, data written with wrong timestamps. So the guard is part of the design, not an afterthought, and it uses more than one layer. The starter refuses to start the application when a shift is configured and the environment is marked as production, checked from a deployment variable that the platform sets, not from a property a developer can edit. Production configuration repositories reject the time.shift properties in review. And monitoring alerts if any production instance ever reports a non-system clock.
Notice the default: if the tier variable is missing, the guard assumes production and refuses. A safety check should fail closed.
4Deciding What Shifts Together
A service's Clock is not the only clock in the system. The database has now() and DEFAULT CURRENT_TIMESTAMP, Kafka stamps records with the broker's or producer's time, an identity provider validates token expiry with its own clock, and third-party APIs know the real date. If the billing service thinks it is 31 January and the database stamps rows with 10 March, queries that compare the two return nonsense.
So the standard defines shift groups. Within a test environment, all of our services shift together, configured from one central value, so they agree with each other. Components we cannot shift are listed explicitly, and the standard says how code must behave with them: services write timestamps from their own Clock instead of relying on database defaults, and comparisons use values from the same source. External systems, such as payment providers, stay on real time and are usually replaced by stubs during shifted runs.
| Component | Clock source | Shifts? | Rule |
|---|---|---|---|
| Our services | injected Clock from starter | Yes, all together | one central offset per environment |
| Database | now(), column defaults | No | write timestamps from the app Clock |
| Kafka record timestamps | producer or broker time | No | business time goes in the payload |
| Identity provider | its own clock | No | use long-lived test tokens |
| Payment and partner APIs | real time | No | use stubs during shifted runs |
5Schedulers Must Use the Same Clock
The last trap is scheduling. If billing runs from a cron trigger at 02:00 on the last day of the month, shifting the business code is not enough; the trigger itself must evaluate the cron expression against the shifted clock, or the job will wait for the real end of the month. Spring's task schedulers can be given a Clock (check your Spring version for the exact setter), and the starter wires the same Clock bean into them.
- Pick target instant2026-01-31T23:00Z
- Pipeline computes offset, sets it centrally
- Restart services in the environmentguard checks tier, logs shifted mode
- Scheduler with shifted Clock fires billing at 02:00 on 1 Feb
- Verify invoices, then reset offset and restart
5 steps in order. Play them, or step through them one at a time.
Even then, a scheduler computes its next run when the job is scheduled, so changing the offset of a running service does not move triggers that are already waiting. The simple rule is to restart services after changing the shift, or to give test runs an endpoint, available only in test tiers, that calls the job directly. For other schedulers, such as Quartz or a platform cron, the same question must be answered: whose clock evaluates the trigger?
6Rolling It Out Across Teams
We roll the standard out in the order of value. First the starter and the lint rules, as warnings, in every service; then fixes in the services that matter most for date-sensitive flows, such as billing, subscriptions and reporting; then lint rules as errors. Each team gets a short checklist: no clock-less now() calls, no database default timestamps for business data, schedulers wired to the Clock bean, and a shifted test for each date-sensitive feature they own. Progress is visible on a dashboard of services that report a compliant clock.
7Follow-up questions
Why use an offset clock rather than a fixed clock in staging?
Because with a fixed clock time never passes, so anything that depends on time moving, such as expiries, retries and schedules, stops working. An offset clock keeps real time flowing from the shifted starting point. Fixed clocks are better for unit and component tests that need exact, repeatable instants.
Could we just change the system time of the staging servers?
It affects everything on the machine, including TLS certificate checks, Kerberos, log timestamps and the database, often in ways that break the environment rather than test it. Containers also share the host's kernel clock. Shifting at the application level, through an injected Clock, is targeted and safe.
How do we stop teams from relying on database now() in business logic?
We make it a review rule and give a pattern to follow: the service sets created_at and similar columns from its Clock in the entity or repository code. A lint rule on migration files can flag DEFAULT now() on business columns, while technical columns such as audit insert times may keep it.
8Conclusion
In this answer, we designed time-shifted testing around one rule, that all production code gets time from an injected Clock, enforced by lint rules. A shared starter provides the Clock bean, offset or fixed through configuration in test tiers, with a fail-closed guard that refuses shifting in production. The standard defines which components shift together, with services writing their own timestamps instead of database defaults and stubs for external systems, and wires the same clock into schedulers so jobs fire at shifted times. A staged rollout with a checklist brings every team along.
Open with
The foundation is one source of time: every service gets the current time from an injected java.time.Clock, and now() calls without a clock are banned in production code by ArchUnit or Error Prone rules, because a single missed call makes a shifted run quietly wrong.
Then, if they want more
The platform team ships a Spring Boot starter that provides the Clock bean. Normally it's Clock.systemUTC, but in test tiers configuration can make it an offset clock, so time still passes from a shifted start like 23:00 on January 31st, or a fixed clock for deterministic tests.
The starter has a guard that fails closed: if the platform's environment tier isn't dev, test or staging, the service refuses to start with a shift, and production config review and monitoring back that up.
Then we decide what shifts together: all our services in an environment shift from one central offset, but databases, Kafka timestamps, identity providers and partner APIs keep real time, so services write business timestamps from their own clock rather than database defaults, and we stub external systems.
Schedulers must use the same clock, for example by giving Spring's task scheduler the Clock bean, otherwise the billing cron still waits for the real month end, and we restart after changing the offset. I'd roll it out starting with billing and subscriptions, with a checklist and a compliance dashboard.
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 "Your organisation wants every service to support time-shifted testing, for example...": https://scrivio-interview-library.netlify.app/java/date-and-time/6e81e75fed/