question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-configure java.time.Clock

See original GitHub issue

Consider autoconfiguring a default java.time.Clock instance.

I was very surprised to see that there is currently is no autoconfigured Clock instance. I know it’s only four lines of code in our application, and yet - every modern application will have them, and therefore Spring Boot can come in rescue:

@Bean
public Clock clock() {
    return Clock.systemDefaultZone();
}

I suppose this was not done because there may be people who want Clock.systemUTC() instead? Indeed, I see that, but with the exception of JEP 400 the JDK is very consistent in using the default timezone/locale etc., e.g. in ZonedDateTime.now() and all its friends.

I strongly believe the above autoconfiguration would positively please and spoil developers without taking risks. It’s a small convenience, that’s it.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Jun 20, 2022

Thanks for sharing your thoughts, @estigma88.

There’s no usage of java.time.Clock in Spring Boot’s public API. There’s a little bit of usage in Spring Framework related to task scheduling and the like. Providing a mutable clock for testing in Spring Framework was proposed and declined. In declining the issue, the Framework team suggested using profiles or a @Primary bean to override the Clock in tests. This is equivalent to the one benefit of auto-configuration that I described above.

Thanks for the suggestion, @JanecekPetr. For the reasons described above, we don’t think that Spring Boot should auto-configure a Clock. If you need one in your application, a minimal amount of code is required to define one. To replace the Clock in tests, use profiles or a @Primary bean.

1reaction
wilkinsonacommented, Jun 16, 2022

I’ve written a few apps recently that have used the java.time APIs including the various now() methods and haven’t needed a Clock for them to be sufficiently testable. As such, I don’t agree that every modern application will need a Clock bean.

The one benefit that I can think of to auto-configuring a Clock is that it could be @ConditionalOnMissingBean and this would then make it easy to replace it with a FixedClock for testing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java & SpringBoot: Overriding system time for Unit, Integration ...
DateTimeProvider consists of the internal clock, and following methods: timeNow() – returns current time, based on internal clock; setTime() – ...
Read more >
Formatting Java Time with Spring Boot using JSON
Step 1 - The goal. I would like to return class Clock , containing LocalDate , LocalTime and LocalDateTime , preinitialized in constructor....
Read more >
How to prevent spring-boot autoconfiguration for spring-web?
When booking a flight when the clock is set back by one hour due to the daylight saving time, how can I know...
Read more >
Spring Boot Reference Documentation
Spring Boot 3.0.1-SNAPSHOT requires Java 17 and is compatible up to and including ... Spring Boot still does its best to auto-configure your...
Read more >
io.micrometer.core.instrument.Clock Java Examples
This page shows Java code examples of io.micrometer.core.instrument.Clock.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found