Quick revision
The short answer
We inject a java.time.Clock into Coupon, defaulting to Clock.systemDefaultZone() or Clock.systemUTC() in production, and call LocalDate.now(clock) instead of LocalDate.now(). A test then passes Clock.fixed(Instant.parse("2025-01-01T00:00:00Z"), ZoneOffset.UTC) to pin today's date, or Clock.offset to move time forward from a base clock. In Spring, we define a Clock bean once and inject it wherever code needs the current time.
What to remember
In this answer, we saw that LocalDate.now() hides two inputs, the system clock and the default zone, so isExpired() could not be tested reliably. Injecting a java.time.Clock and calling LocalDate.now(clock) made both explicit. Tests use Clock.fixed to set today and Clock.offset to move forward, and the clock's zone decides which date an instant falls on. In Spring, a single Clock bean provides time to the whole application and a test configuration replaces it.
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 inject a java.time.Clock into Coupon, defaulting to Clock.systemDefaultZone() or Clock.systemUTC() in production, and call LocalDate.now(clock) instead of LocalDate.now(). A test then passes Clock.fixed(Instant.parse("2025-01-01T00:00:00Z"), ZoneOffset.UTC) to pin today's date, or Clock.offset to move time forward from a base clock. In Spring, we define a Clock bean once and inject it wherever code needs the current time.
Why this question is asked
Code that reads the current time directly is one of the most common reasons tests are flaky or missing. A test for isExpired() written today passes until the expiry date arrives, and then fails in CI one morning for no visible reason. Interviewers ask this small refactoring because it shows whether we know the standard tool for it, java.time.Clock, and whether we understand the zone that comes with it. In this answer, we will see why the original code cannot be tested, introduce Clock, rewrite Coupon, write tests with a fixed and an offset clock, and wire the clock in Spring.
1Why the Original Code Cannot Be Tested
Here is the class from the question, with a constructor added.
LocalDate.now() has two hidden inputs: the real system clock and the JVM's default time zone. The test cannot control either. So it can only test 'today', its result changes with the calendar, and it can even differ between a developer in Tokyo and a CI server in Virginia on the same evening. We cannot write a test for 'expired the day after expiry' at all.
2A Quick Recap of java.time.Clock
java.time.Clock is a small abstract class whose job is to answer two questions: what is the current instant, and which time zone should be used to turn it into a date or a time? Every now() method in java.time has an overload that takes a Clock: Instant.now(clock), LocalDate.now(clock), ZonedDateTime.now(clock). The no-argument versions simply use the system clock with the default zone.
Java provides the implementations we need. Clock.systemUTC() and Clock.systemDefaultZone() read the real clock. Clock.fixed(instant, zone) always returns the same instant, which is perfect for tests. Clock.offset(base, duration) returns another clock's time shifted by a fixed amount, which lets a test say 'the same as before, but 40 days later'.
3Rewriting Coupon With an Injected Clock
We pass the clock in through the constructor and use it in the one place that needs the current date.
Notice that the behaviour is unchanged for existing callers: the one-argument constructor still uses the system clock and the default zone. But the choice of default now deserves a thought. LocalDate.now(clock) uses the clock's zone to decide what today is, so Clock.systemUTC() means 'expires at midnight UTC' and Clock.systemDefaultZone() means 'midnight wherever the server is'. A coupon for customers in one country might really want a clock in that country's zone, such as Clock.system(ZoneId.of("Europe/Paris")).
4Setting the Date in a Test
Now a test can decide exactly what today is. We fix the clock to a moment, and the date follows from the clock's zone.
| Clock in the test | LocalDate.now(clock) | isExpired() for expiry 2025-01-01 |
|---|---|---|
| fixed 2025-01-01T00:00Z, UTC | 2025-01-01 | false |
| offset by 1 day | 2025-01-02 | true |
| same instant, America/New_York | 2024-12-31 | false |
The first two assertions test the boundary that matters: a coupon is still valid on its expiry day and expired the day after. These tests will give the same result next year and on any machine. The last line shows why the zone is part of the clock: the same instant is still 31 December in New York, so the same coupon would not yet be at its expiry day there.
5Wiring the Clock in Spring
In a Spring application, we do not want every class to construct its own clock. We define one Clock bean in a configuration class and let Spring inject it, so production code shares a single source of time, and tests replace that one bean.
Notice that the service version takes the clock in the service rather than in the coupon. Coupon might be a JPA entity or a record created from JSON, where constructor injection of a clock is awkward. An even simpler design for such value objects is a pure method that receives today's date, isExpiredOn(LocalDate today), and leaves the clock to the service that calls it.
6Follow-up questions
Why not mock LocalDate.now() with a mocking library instead?
Some libraries can mock static methods, but it is slower, more fragile and hides the dependency. A Clock parameter makes the time dependency visible in the constructor, works with any test framework, and needs no special tooling. It is also exactly what the java.time designers intended Clock for.
Is it a problem that Clock.fixed never moves?
Only for tests that need time to pass during the test. For those, we use Clock.offset to create a later clock, or a small mutable test clock that we can advance. For most date logic, a fixed clock per test case is the clearest choice.
Which zone should the production clock use?
The zone that defines 'today' for the business rule. Services often use Clock.systemUTC() as the bean and apply the customer's or market's zone explicitly with clock.withZone(zone) when a rule depends on local dates. What matters is that the choice is explicit rather than taken from the server.
7Conclusion
In this answer, we saw that LocalDate.now() hides two inputs, the system clock and the default zone, so isExpired() could not be tested reliably. Injecting a java.time.Clock and calling LocalDate.now(clock) made both explicit. Tests use Clock.fixed to set today and Clock.offset to move forward, and the clock's zone decides which date an instant falls on. In Spring, a single Clock bean provides time to the whole application and a test configuration replaces it.
Open with
The problem is that LocalDate.now() reads two things we can't control in a test: the system clock and the JVM's default time zone. So I'd inject a java.time.Clock into Coupon through the constructor and call LocalDate.now(clock) instead.
Then, if they want more
For existing callers I'd keep a constructor that defaults to Clock.systemDefaultZone(), or Clock.systemUTC() if the business defines dates in UTC, because the clock's zone decides what today is. In the test I'd create Clock.fixed with Instant.parse of 2025-01-01T00:00:00Z and ZoneOffset.UTC, and a coupon expiring on January 1st, and assert it isn't expired on that day.
Then I'd use Clock.offset of that clock by one day and assert it is expired, so I test the boundary. I'd also point out that the same instant with an America/New_York zone is still December 31st, which is exactly why the zone belongs in the clock.
In Spring I'd define one Clock bean, inject it into services, and override it with a fixed clock in tests using a test configuration. If Coupon is an entity or a record, I might keep it pure with a method like isExpiredOn(today) and let the service pass LocalDate.now(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 "Rewrite this to be testable:": https://scrivio-interview-library.netlify.app/java/date-and-time/45445b8e9b/