Quick revision
The short answer
We make java.time.Clock the single abstraction for the current time and ban now() calls without a clock in domain code, enforced with ArchUnit or Error Prone rules. Tests use a mutable test clock that extends Clock and can be advanced, so we can step 15 minutes past a token's issue or cross midnight for a daily rollup. The clock's zone is used explicitly, and we keep wall time separate from elapsed time, because timeouts and sleeps run on nanoTime or a scheduler that need their own control.
What to remember
In this answer, we chose java.time.Clock as the single source of current time, injected everywhere and configured once as Clock.systemUTC(). A shared, thread-safe MutableClock lets tests advance time, so token expiry and daily rollups are tested at their exact boundaries without sleeping. Code that cares about local days names its zone explicitly, and ArchUnit or Error Prone rules ban zone-less now() calls in domain code. Elapsed time inside libraries is a separate concern, controlled with nanoTime-based hooks, short real timeouts or direct calls to job methods.
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 make java.time.Clock the single abstraction for the current time and ban now() calls without a clock in domain code, enforced with ArchUnit or Error Prone rules. Tests use a mutable test clock that extends Clock and can be advanced, so we can step 15 minutes past a token's issue or cross midnight for a daily rollup. The clock's zone is used explicitly, and we keep wall time separate from elapsed time, because timeouts and sleeps run on nanoTime or a scheduler that need their own control.
Why this question is asked
Time-dependent behaviour is everywhere in a real codebase: tokens expire, carts are abandoned after an hour, rollups run daily, trials end after 14 days. Without a design for time, each test either sleeps, which is slow and flaky, or does not exist. Interviewers ask this design question to see whether we can choose one abstraction, define the rules around it and make the rules enforceable across a team. In this answer, we will choose the abstraction, build a mutable test clock, test token expiry and a daily rollup with it, enforce the rules with static analysis, and deal with the part Clock cannot control: elapsed time.
1Choosing the Abstraction: java.time.Clock
We could invent our own TimeProvider interface, but Java already has the right one. java.time.Clock answers 'what instant is it now?' and 'in which zone?', and every now() method in java.time accepts it: Instant.now(clock), LocalDate.now(clock), ZonedDateTime.now(clock). Libraries and colleagues already know it, and Spring can inject it like any other bean.
So the rule for the codebase is short: domain and application code never asks the system for the time directly. It receives a Clock and passes it to now(). Production configures Clock.systemUTC() once; tests supply a clock they control.
2A Mutable Test Clock
Clock.fixed is good for tests that check behaviour at one moment. For behaviour that depends on time passing, we want a clock the test can move forward. Clock is an abstract class with three methods to implement, so a mutable version is short. We keep the current instant in an AtomicReference so that clocks created with withZone share the same time, and so that a test thread and application threads see updates safely.
Notice that the class lives in shared test tooling, not in each team's tests, so everyone uses the same well-tested implementation. Also notice there is no way to move time backwards by accident in advance(); setInstant exists for the rare test that needs it.
3Testing Token Expiry After 15 Minutes
Token expiry is an elapsed-time rule expressed with instants: a token issued at T is valid until T plus 15 minutes. With the mutable clock, the test walks right up to the boundary and across it in two lines, with no sleeping.
9 messages in order. Play them, or step through them one at a time.
4Testing a Daily Rollup: The Zone Is Part of the Rule
A daily rollup depends on which calendar day it is, and a calendar day depends on a zone. Clock carries a zone, and LocalDate.now(clock) uses it, which is convenient but easy to get wrong if the zone is left implicit. Our rule is that code that cares about a local day says which zone it means, for example clock.withZone(market.zone()), rather than relying on whatever zone the injected clock happens to have.
Notice that the day changed at 23:00 UTC, which is midnight in Paris in winter. A rollup test that assumed UTC would have run an hour late, which is exactly the kind of bug these tests exist to catch. For the rollup itself, the test advances the clock past midnight and calls the job's method directly, rather than waiting for a scheduler.
5Enforcing the Rules
Rules that rely on code review are forgotten within months. So we enforce them in the build. ArchUnit can fail a test when classes in domain or application packages call the no-argument now() methods or the system clock factories; Error Prone has checks for java.time calls that depend on the default zone. Configuration classes are the only permitted exception.
6Wall Time Versus Elapsed Time
Clock controls wall time, the answer to 'what time is it?'. It does not control elapsed time inside libraries: HTTP client timeouts, Thread.sleep, CompletableFuture.orTimeout and connection pool timers all use System.nanoTime or the operating system's timers. Advancing our MutableClock does not make an HTTP timeout fire.
So the design keeps the two apart. Business rules about time, such as expiry, trials and rollups, use Clock and are tested by advancing it. Technical timeouts stay in infrastructure code and are tested with short real timeouts or with the library's own hooks; for example, the Caffeine cache accepts a Ticker, a nanosecond source we can fake. Scheduled jobs are tested by calling the job method directly with a controlled clock, while the scheduler's wiring is tested separately, once.
| Kind of time | Example | Source | Controlled in tests by |
|---|---|---|---|
| Wall time in a business rule | token expiry, trial end | injected Clock | MutableClock.advance |
| Local business day | daily rollup | Clock + explicit zone | advance past midnight in that zone |
| Elapsed time in a library | HTTP timeout, cache expiry | nanoTime, Ticker | short real timeouts or a fake Ticker |
| Trigger time | @Scheduled job | scheduler | call the job method directly |
7Follow-up questions
Why not let tests use Thread.sleep for short durations?
Because sleeping makes tests slow and still flaky. A 15-minute rule cannot be tested by sleeping at all, and a one-second rule tested with a one-second sleep fails on a busy CI machine. Advancing a clock is instant and deterministic.
Should the mutable clock be thread-safe?
Yes, because the code under test often reads the clock from other threads, for example in a background worker or an async handler. An AtomicReference, or a volatile field with atomic updates, makes advances visible to every thread.
How do you handle third-party code that calls Instant.now() internally?
We cannot change it, so we keep it at the edge of the system and avoid using its notion of time for business rules. If its behaviour matters in tests, we wrap it behind our own interface or test it with real time on a short scale.
8Conclusion
In this answer, we chose java.time.Clock as the single source of current time, injected everywhere and configured once as Clock.systemUTC(). A shared, thread-safe MutableClock lets tests advance time, so token expiry and daily rollups are tested at their exact boundaries without sleeping. Code that cares about local days names its zone explicitly, and ArchUnit or Error Prone rules ban zone-less now() calls in domain code. Elapsed time inside libraries is a separate concern, controlled with nanoTime-based hooks, short real timeouts or direct calls to job methods.
Open with
I'd use java.time.Clock as the only abstraction, because every now method in java.time already takes one and Spring can inject it. The rule is that domain and application code never calls now() without a clock; production defines one bean, Clock.systemUTC().
Then, if they want more
For tests, shared tooling provides a mutable clock that extends Clock, keeps the instant in an AtomicReference so it is thread-safe and shared by withZone copies, and has an advance method. Then token expiry is easy: issue a token at 08:00, advance 14 minutes 59 seconds and assert valid, advance one more second and assert expired, with no sleeping.
For daily rollups the zone matters, so code that needs a business day says which zone, for example LocalDate.now(clock.withZone(paris)), and tests advance past midnight in that zone. I'd enforce the rules in the build with ArchUnit rules banning Instant.now, LocalDate.now and currentTimeMillis in domain packages, or Error Prone's default-time-zone check.
Finally I'd keep wall time and elapsed time apart: library timeouts and sleeps use nanoTime, which the clock doesn't control, so those are tested with short real timeouts or fake tickers, and scheduled jobs are tested by calling the job method directly with the controlled clock.
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 team wants to test behaviour that depends on time passing, such as token expiry...": https://scrivio-interview-library.netlify.app/java/date-and-time/9ec73f0635/