Is there any way to convert ZoneId to ZoneOffset in Java 8?
Solution 1
Here is how you can get ZoneOffset
from ZoneId
:
Instant instant = Instant.now(); //can be LocalDateTime
ZoneId systemZone = ZoneId.systemDefault(); // my timezone
ZoneOffset currentOffsetForMyZone = systemZone.getRules().getOffset(instant);
NB: ZoneId
can have different offset depending on point in time and the history of the particular place. So choosing different Instants would result in different offsets.
NB2: ZoneId.of()
can return a ZoneOffset
instead of ZoneId
if UTC+3
/GMT+2
/etc is passed as opposed to a time zone like Africa/Cairo
. So if UTC/GMT offsets are passed then historical/geographical/daylight-saving information of the Instant
won't be taken into account - you'll simply work with the specified offset.
Solution 2
tl;dr
ZonedDateTime.now(
ZoneId.of( "America/Montreal" )
)
…of current default time zone…
ZonedDateTime.now(
ZoneId.systemDefault()
)
Details
The Answer by Stanislav Bshkyrtsev correctly and directly answers your Question.
But, there are larger issues involved, as suggested in the Answer by Jon Skeet.
LocalDateTime
I don't find the way to convert epoch second to LocalDateTime
LocalDateTime
purposely has no concept of time zone or offset-from-UTC. Not likely what you want. The Local…
means any locality, not any one particular locality. This class does not represent a moment, only potential moments along a range of about 26-27 hours (the range of time zones around the globe).
Instant
No need to start with epoch seconds if you are trying to get current time. Get the current Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = Instant.now();
Inside of that Instant
is a count of nanoseconds-from-epoch. But we do not really care.
See also, What's the difference between Instant and LocalDateTime?
ZonedDateTime
If you want to see that moment through the lens of a particular region’s wall-clock time, apply a ZoneId
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "Europe/Paris" );
ZonedDateTime zdt = instant.atZone( z );
As a shortcut, you can do directly to the ZonedDateTime
.
ZonedDateTime zdt = ZonedDateTime.now( z );
A ZonedDateTime
has an Instant
within it. Call zdt.toInstant()
to get the same moment in time as a basic value in UTC. Same number of nanoseconds-since-epoch either way, as a ZonedDateTime
or as a Instant
.
Seconds-since-epoch given
If you are given a count of seconds-since-epoch, and the epoch is the first moment of 1970 in UTC (1970-01-01T00:00:00Z
), then feed that number to Instant
.
long secondsSinceEpoch = 1_484_063_246L ;
Instant instant = Instant.ofEpochSecond( secondsSinceEpoch ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Solution 3
There is no one-to-one mapping. A ZoneId defines a geographic extent in which a set of different ZoneOffsets is used over time. If the timezone uses daylight saving time, its ZoneOffset will be different between summer and winter.
Furthermore, the daylight saving time rules may have changed over time, so the ZoneOffset could be different for e.g. 13/10/2015 compared to 13/10/1980.
So you can only find the ZoneOffset for a ZoneId on a particular Instant.
See also https://en.wikipedia.org/wiki/Tz_database
Solution 4
As the documentation says, "This is primarily intended for low-level conversions rather than general application usage."
Going via Instant
makes perfect sense to me - your epoch second is effectively a different representation of an Instant
, so convert to an Instant
and then convert that into a particular time zone.
Solution 5
I hope the first two lines of my solution below are helpful. My problem was I had a LocalDateTime
and the name of a time zone, and I needed an instant
so I could build a java.util.Date
, because that's what MongoDB wanted. My code is Scala, but it's so close to Java here I think there should be no problem understanding it:
val zid = ZoneId.of(tzName) // "America/Los_Angeles"
val zo: ZoneOffset = zid.getRules.getOffset(localDateTime) // ⇒ -07:00
// 2017-03-16T18:03
val odt = OffsetDateTime.of(localDateTime, zo) // ⇒ 2017-03-16T18:03:00-07:00
val instant = odt.toInstant // ⇒ 2017-03-17T01:03:00Z
val issued = Date.from(instant)
Related videos on Youtube
Renkai
Updated on December 12, 2021Comments
-
Renkai over 2 years
I have an epoch second and a zoneId (see
method1
below).It can be convert to
LocalDateTime
with system default zoneId, but I don't find the way to convert epoch second toLocalDateTime
(seemethod2
below), because there is noZoneOffset.systemDefault
. I think it's obscure.import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset} val epochSecond = System.currentTimeMillis() / 1000 // method1 LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), ZoneId.systemDefault()) // method2 LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.MAX)
NOTE
The source code presented above is Scala.
-
Snackoverflow over 5 yearsWhat language/dialect are you using to make the bulk import
import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset}
work? It's anunexpected token
for me in Java 11. -
donatas M about 5 yearsCan be Scala, but that's not really important here
-
febot about 5 yearsI have discussed the theory behind
java.time
at length here: stackoverflow.com/a/56508200/145989
-
-
Kai Moritz over 8 yearsI know that, but how is it done?If I know the LocaDate and have a ZoneId, how do I convert it into a ZoneOffset. My use-case is a string that contains a time and a zone. I want to parse the string into an OffsetTime, without droping the zone information.
-
gknauth over 7 yearsI posted this answer here because this was the page that came up when I was searching for a way to do what I eventually figured out.
-
msangel about 4 yearsThis gives incorrect value if current instant is in DST, but at the moment of calculation it was without DST
-
msangel about 4 yearsI checked that and it works depending on another condition. Updated answer with that.
-
Vitamon over 3 yearslocalZoneOffset = ZoneOffset.ofTotalSeconds(TimeZone.getTimeZone(localTimeZone).getRawOffset / 1000)