Created
September 11, 2018 16:20
-
-
Save iamitshri/5db90b5ffdc74807fc7b04bd74773e93 to your computer and use it in GitHub Desktop.
Spring Data auditing using user supplied DateTimeProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wgu.dm.audit.jpa; | |
import java.time.Instant; | |
import java.time.ZoneOffset; | |
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
import org.springframework.data.auditing.DateTimeProvider; | |
import org.springframework.stereotype.Component; | |
@Component("UtcDateTimeProvider") | |
public class DateTimeProviderImpl implements DateTimeProvider { | |
@Override | |
public Calendar getNow() { | |
return GregorianCalendar.from(Instant.now().atZone(ZoneOffset.UTC)); | |
} | |
// Another way to get Zoned Date time for UTC time: ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("UTC")) | |
} | |
/* | |
In your spring boot app's Application.java, add this annotation to use this class | |
@EnableJpaAuditing(dateTimeProviderRef="UtcDateTimeProvider") | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot!