Java – get localdatetime from seconds, including time zone

I have time from our computer "day 0" (at midnight on January 1, 1970) in a few seconds. I want to convert it to a localdatetime instance This code does this:

LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds,ZoneOffset.UTC)

However, I need this localdatetime in my time zone Set zoneoffset UTC changed to zineoffset Of ("2") will make localdatetime display the correct time. (for, we say Paris as of August 31, 2015)

But this is more or less an imperfect solution because it doesn't take care of daylight saving time

So I tried and found the zoneddatetime class It doesn't help me either, but I'm stuck here:

LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(seconds,ZoneOffset.UTC);
zoneddatetime zoneddatetime = zoneddatetime.of(localDateTime,ZoneId.systemDefault());

This doesn't work at all because zoneddatetime contains a localdatetime and then a Localtime, which is my UTC time, not my current time zone

So how do you get a localdatetime from (preferably) the system time zone, including daylight saving time (if required), from UTC seconds to epoch values?

Solution

You need to specify zoneid to take into account the daylight saving time of the geographical area Get a list of available zone IDS:

System.out.println(ZoneId.getAvailableZoneIds());

With the correct zone ID, you will be able to access daylight saving time rules

For example, in Germany (UTC 1), they conducted their first daylight saving time in 1940 (UTC 2) In 1947, they introduced midsummer time, two hours ahead of standard winter time (= UTC 3) The following are examples of the summer of 1940, 1947, 1970 (without daylight saving time) and this morning:

Instant[] instants = new Instant[] {
        LocalDateTime.of(1939,6,1,0).toInstant(ZoneOffset.UTC),LocalDateTime.of(1940,LocalDateTime.of(1947,Instant.ofEpochSecond(0),// 1970-01-01T00:00:00 UTC
        LocalDateTime.of(1970,Instant.Now().truncatedTo(ChronoUnit.DAYS),// 2015-08-31T00:00:00 UTC
};

for (Instant instant : instants) {
    LocalDateTime inBerlin = LocalDateTime.ofInstant(instant,ZoneId.of("Europe/Berlin"));
    System.out.println("UTC    " + instant.atOffset(ZoneOffset.UTC).toLocalDateTime());
    System.out.println("BERLIN " + inBerlin);
    System.out.println();
}

This will print as follows:

UTC    1939-06-01T00:00
BERLIN 1939-06-01T01:00

UTC    1940-06-01T00:00
BERLIN 1940-06-01T02:00

UTC    1947-06-01T00:00
BERLIN 1947-06-01T03:00

UTC    1970-01-01T00:00
BERLIN 1970-01-01T01:00

UTC    1970-06-01T00:00
BERLIN 1970-06-01T01:00

UTC    2015-08-31T00:00
BERLIN 2015-08-31T02:00

As you can see, the rules are implemented correctly The first daylight saving time in Berlin was 1940. In 1947, daylight saving time was added by another hour Between 1950 and 1980, there was no daylight saving time at all In 1980, many European countries had a carefully planned start of daylight saving time

Oh, answer your question:

LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds),ZoneId.systemDefault())
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>