Java Calendar.roll and CST for daytime changes

I wonder if Calendar.roll adheres to its javadoc contract:

The following snippet

    final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("CST"));
    cal.setTimeInMillis(1457928024812l);

    System.out.println(cal.getTime());
    cal.roll(Calendar.HOUR_OF_DAY, true);
    System.out.println(cal.getTime());

outputs the following output:

Sun Mar 13 23:00:24 CDT 2016
Sun Mar 13 23:00:24 CDT 2016

March 13, 2016 there was a change in daylight at 2 a.m. (from CST to CDT). The javadoc of roll states that roll "adds a unit of time", and units of time are not added here. Is this the expected behavior of this method?

EDIT:

I reported this as an error. For more information, see the Link to the corresponding OpenJDK ticket: https://bugs.openjdk.java.net/browse/JDK-8152077

+4
source share
2 answers

, -, ​​ roll. !

:

  • SimpleDateFormat, , , getTime Date, .

  • America/Chicago, CST, .

  • 23- , 0 . , add roll. . vs roll. add .

  • spring -forward 23 . roll, , , ( 2:00 , 3:00). , 23, 0 .

  • 25 . , roll , 1 0, ( - 2:00 , 1:00).

, , - . , .

, , , Joda Time Java 7 java.time Java 8 .

+2

:

  • continent/region. 3-4 , , .
  • java.time, Java 8 . . Tutorial. , java.util.Date/.Calendar.

An Instant - UTC nanosecond. (ZoneId), ZonedDateTime.

Long input = 1457928024812L;
Instant instant = Instant.ofEpochMilli ( input );

ZoneId zoneId = ZoneId.of ( "America/Chicago" );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant, zoneId );

ZonedDateTime zdtHourLater = zdt.plusHours ( 1 );

.

System.out.println ( "input: " + input + " |  instant: " + instant + " | zoneId: " + zoneId + " | zdt: " + zdt + " | zdtHourLater: " + zdtHourLater );

: 1457928024812 | : 2016-03-14T04: 00: 24.812Z | zoneId: / | zdt: 2016-03-13T23: 00: 24.812-05: 00 [/] | zdtHourLater: 2016-03-14T00: 00: 24.812-05: 00 [/]

, java.time. , , 11 13- 14-.

(DST)

DST .

DST

1:59 3:59 , .

ZonedDateTime zdtBeforeTwoAm = ZonedDateTime.of ( 2016, Month.MARCH.getValue ( ), 13, 1, 59, 0, 0, zoneId );
ZonedDateTime zdtBeforeTwoAmPlus = zdtBeforeTwoAm.plusHours ( 1 );

.

System.out.println ( "zdtBeforeTwoAm: " + zdtBeforeTwoAm + " |  zdtBeforeTwoAmPlus: " + zdtBeforeTwoAmPlus );

zdtBeforeTwoAm: 2016-03-13T01: 59-06: 00 [/] | zdtBeforeTwoAmPlus: 2016-03-13T03: 59-05: 00 [/]

2

2 ( ). , java.time , 3 AM.

ZonedDateTime zdtTwoAm = ZonedDateTime.of ( 2016, Month.MARCH.getValue ( ), 13, 2, 0, 0, 0, zoneId );

.

System.out.println ("zdtTwoAm: " + zdtTwoAm );

zdtTwoAm: 2016-03-13T03: 00-05: 00 [/]

+1

All Articles