spring-data-commons: Auditing should support Java 8 Date & Time types [DATACMNS-411]

Nick Williams opened DATACMNS-411 and commented

This may or may not be in the right project. I think it is, but it may need to be moved to Spring Data Commons.

Currently, fields annotated @CreatedDate and @LastModifiedDate can be Joda Time’s DateTime or (I think) java.util.Date, java.util.Calendar, or java.sql.Timestamp. If it doesn’t support these last three types, it should.

Auditing should also support the Java 8 Date & Time API. Fields modified with one of these two annotations should be able to be java.time.Instant, java.time.LocalDateTime, java.time.OffsetDateTime, and java.time.ZonedDateTime


Affects: 1.7 M1 (Codd), 1.6.3 (Babbage SR2)

Issue Links:

  • SPR-11259 Add Converter implementations that convert legacy Date instances into JDK 8 date/time types (“depends on”)

  • DATAJPA-592 Auditing annotations not working when applied to methods

  • DATACMNS-307 Remove JodaTime dependency

Referenced from: commits https://github.com/spring-projects/spring-data-commons/commit/51b128fac1c17c8327d6fdd8c7ef87b039ac6c60

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Reactions: 1
  • Comments: 30 (4 by maintainers)

Most upvoted comments

I can’t believe that this is still not working in 2021… For everyone tired of all this, just create this configuration class:

(Kotlin)

@Configuration
@EnableJpaAuditing(dateTimeProviderRef = "dateTimeProvider")
class PersistenceConfig {
    @Bean 
    fun dateTimeProvider() = DateTimeProvider { Optional.of(OffsetDateTime.now()) }
}

Using LocalDateTime as the basis certainly was a mistake an we should probable fix that.

That said, discussing this on an issue that was closed 8 years ago isn’t helpful either.

For the record: The issue was closed because we do support Java 8 java.time classes to the extend that we actually want to: Anything with time zone is really out of place in my and not only in my opinion, especially since support for those in JPA and JDBC is … sketchy.

In order to get this ticket here it’s well earned rest:

  • If you want us to change the implementation basis for Auditing time stamps from LocalDate to Instant please create a ticket for it. For that one a PR even without a ticket has also a good chance of acceptance.
  • If you think you really need support for OffsetDateTime you are free to create a fresh ticket as well. Just be aware that you’ll probably have to do some convincing.

Yes, fully agree. UTC Instant for point-in-time cases (apply time zone in presentation layer). LocalDateTime + separate time zone ID for “human” date+time (e.g., meeting at 9am).

And indeed, using Instant for the audit columns works out of the box 👍 Though I then find it strange that the default org.springframework.data.auditing.CurrentDateTimeProvider provides a LocalDateTime. Technically, it’s not possible to convert a LocalDateTime into an Instant unless you assume a time zone (either system default or UTC). org.springframework.data.convert.Jsr310Converters.LocalDateTimeToInstantConverter silently assumes the system default time zone.

In any case, my problem is solved (I have switched to Instants as they capture the nature of audit timestamps best). Thanks a lot for the discussion 😃

tl;dr for every one else stumbling across this ticket: use java.time.Instant for the audit columns; not java.time.OffsetDateTime