Is there any way to convert ZoneId to ZoneOffset i

2019-01-22 17:05发布

问题:

I have an epoch second and a zoneId,by method1.It can be convert to LocalDateTime with system default zoneId,but I don't find the way to convert epoch second to LocalDateTime by method2,because there is no ZoneOffset.systemDefault.I think it's obscure.

import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset}

val epochSecond = System.currentTimeMillis() / 1000

LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), ZoneId.systemDefault())//method1
LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.MAX)//method2

回答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.



回答2:

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



回答3:

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.

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.

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 ) ;


回答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.



回答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)


回答6:

The following returns the amount of time in milliseconds to add to UTC to get standard time in this time zone:

TimeZone.getTimeZone(ZoneId.of("Europe/Amsterdam")).getRawOffset()