Auto-configure java.time.Clock
See original GitHub issueConsider 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:
- Created a year ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 theClock
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 theClock
in tests, use profiles or a@Primary
bean.I’ve written a few apps recently that have used the
java.time
APIs including the variousnow()
methods and haven’t needed aClock
for them to be sufficiently testable. As such, I don’t agree that every modern application will need aClock
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 aFixedClock
for testing.