Link to home
Start Free TrialLog in
Avatar of ronan_40060
ronan_40060Flag for United States of America

asked on

Convert Java LocalDate to date time stamp UTC format

 My Entity Class AppAuthorization.java has the below column EFFECTIVE_START_DATE which is defined as EFFECTIVE_START_DATE DATE(8) NOT NULL column  of APP_AUTHORIZATION Table in H2 DB
 @Column(name = "EFFECTIVE_START_DATE", nullable = false)
 private LocalDate effectiveStartDate;

Open in new window


My question is how can I save  effectiveStartDate in UTC date time stamp format in APP_AUTHORIZATION Table   using Java 8 ?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That's supported out of the box in JPA >= 2.2 and Hibernate >= 5.3 apparently
Avatar of ronan_40060

ASKER

Thank you CEHJ ,
Im using below code to convert LocalDate to UTC datetime Stamp . Is this correct ?

appAuthorization.setEffectiveStartDate(LocalDate.now(ZoneOffset.UTC));
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
even I found that  Instant.now() can be used to get time in UTC format so which one is better ? This one or above
 in my earlier post > 

appAuthorization.setEffectiveStartDate(LocalDate.from(Instant.now()));

Open in new window

Hmm. I wonder what would happen if the 'system UTC' clock were not available with Instant.now()
looks like LocalDate.now(ZoneOffset.UTC) is a better solution
I'd guess, yes
is there any way to substract a milli second from
LocalDate.now(ZoneOffset.UTC)

Open in new window