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)
I can’t believe that this is still not working in 2021… For everyone tired of all this, just create this configuration class:
(Kotlin)
Using
LocalDateTimeas 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.timeclasses 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:
LocalDatetoInstantplease create a ticket for it. For that one a PR even without a ticket has also a good chance of acceptance.OffsetDateTimeyou 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
Instantfor 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
Instantfor the audit columns works out of the box 👍 Though I then find it strange that the defaultorg.springframework.data.auditing.CurrentDateTimeProviderprovides aLocalDateTime. Technically, it’s not possible to convert aLocalDateTimeinto anInstantunless you assume a time zone (either system default or UTC).org.springframework.data.convert.Jsr310Converters.LocalDateTimeToInstantConvertersilently 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.Instantfor the audit columns; notjava.time.OffsetDateTime