I want to get current ZoneOffset of my system.
I tried to do that but I couldn't find a way.
Also, I was looking for a solution but I didn't find any.
Is it possible to do that in Java?
EDIT:
My question is different to this. I would like to know the current system UTC, not how to convert between TimeZone offset representation or TimeZone storing.
ZoneOffset extends ZoneId and defines the fixed offset of the current time-zone with GMT/UTC, such as +02:00. This means that this number represents fixed hours and minutes, representing the difference between the time in current time-zone and GMT/UTC: LocalDateTime now = LocalDateTime.
The systemDefault() method of the ZoneOffset class in Java is used to return the system default time-zone. Parameters: This method does not accepts any parameters. Return Value: This method returns the system default time-zone.
Your request has two parts:
ZoneId.systemDefault()
Instant.now()
These are tied together using ZoneRules
, to get the following:
ZoneOffset o = ZoneId.systemDefault().getRules().getOffset(Instant.now());
For simplicty, you might want to use OffsetDateTime
:
ZoneOffset o = OffsetDateTime.now().getOffset();
This works because OffsetDateTime.now()
uses both ZoneId.systemDefault()
and Instant.now()
internally.
Yes, it's possible:
ZoneOffset.systemDefault().getRules().getOffset(Instant.now())
or you can replace Instant instance with LocalDateTime instance:
ZoneOffset.systemDefault().getRules().getOffset(LocalDateTime.now())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With