简体   繁体   中英

Inconsistency with ZonedDateTime.of()

In two different parts of my code I use ZonedDateTime.of() to create an instance. In both I use the same ZoneId which is defined with ZoneId zoneId = ZoneId.of("Brazil/Acre");

Then when comparing the instances with before() and after() I get wrong results. I observed that the timezone of both are surprisingly different. Printing one gives

0013-06-20T00:00-04:31:12[Brazil/Acre]

And printing the other gives

2013-06-20T00:00:21-04:00[Brazil/Acre]

Note the change: 04:31:12[Brazil/Acre] in one but 04:00[Brazil/Acre] in the other (the latter seems correct because of the exact 4:00 hours difference from Greenwich clock).

I created both in the following way:

ZonedDateTime time = ZonedDateTime.of(year, month, day, hours, minutes, seconds, 0, zoneId);

I would be so happy to learn what went wrong with my code.

So you use year 13 instead of 2013:

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class X {
    public static void main(String[] s){
        ZoneId zoneId = ZoneId.of("Brazil/Acre");
        ZonedDateTime time = ZonedDateTime.of(13, 6, 20, 4, 31, 12, 0, zoneId);
        System.out.println(time); // gives 0013-06-20T04:31:12-04:31:12[Brazil/Acre]
        time = ZonedDateTime.of(2013, 6, 20, 4, 31, 12, 0, zoneId);
        System.out.println(time); //gives 2013-06-20T04:31:12-04:00[Brazil/Acre]
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM