Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert Java ZonedDateTime to OffsetDateTime with default time zone offset?

Tags:

java

java-time

Say I have a ZonedDateTime of 2018-10-30T18:04:58.874Z : How can I convert this to OffsetDateTime 2018-10-30T13:04:58.874-05:00

I'd prefer the offset to be the default/system offset, for example pulled from OffsetDateTime.now().

like image 397
Greg Avatar asked Sep 03 '26 04:09

Greg


1 Answers

From your ZonedDateTime you need to specify in which other zone you want your OffsetDateTime, precise the zone and them use .toOffsetDateTime() :

ZonedDateTime z = ZonedDateTime.parse("2018-10-30T18:04:58.874Z");
System.out.println(z); //2018-10-30T18:04:58.874Z

OffsetDateTime o = z.withZoneSameInstant(ZoneId.of("UTC-5")).toOffsetDateTime();
System.out.println(o); //2018-10-30T13:04:58.874-05:00
like image 97
azro Avatar answered Sep 04 '26 19:09

azro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!