tzortzik
tzortzik

Reputation: 5133

Daylight saving time from ZonedDateTime not working as expected

I have the following function

public String getDateAndTimeInUserFormat(String value, DateTimeFormat inputFormat) {
    return ZonedDateTime.parse(value, DateTimeFormatter.ofPattern(inputFormat.getFormat()))
            .withZoneSameInstant(ZoneId.of(user().getTimezone()))
            .format(DateTimeFormatter.ofPattern(user().getDateFormat() + " " + user().getTimeFormat()));
}

and the following test

@Test
public void getDateAndTimeInUserFormat() throws Exception {
    I18NService service = prepareForUserFormatCheck("####.000", "Brazil/Acre");
    assertEquals(service.getDateAndTimeInUserFormat("2017-05-06T20:52:52+0200"),"06-05-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-12-06T20:52:52+0200"),"06-12-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-01-06T20:52:52+0200"),"06-01-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-03-06T20:52:52+0200"),"06-03-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-07-06T20:52:52+0200"),"06-07-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-09-06T20:52:52+0200"),"06-09-2017 13:52");
}

The test passes but it shouldn't. From my undersntading I should've received some results with a difference of one hour. Why do I have the same time for all these tests?

Upvotes: 0

Views: 1502

Answers (2)

fasd
fasd

Reputation: 3

One easy way to check DST transition dates is to get the ZoneRules object from the ZoneId:

ZoneRules rules = ZoneId.of("Brazil/East").getRules();

Then you can check the list returned by rules.getTransitions(), to see all the dates when those changes occurred, and also the offset before and after the change.

Some zones don't have the transitions list, but have transition rules instead, which can be checked with rules.getTransitionRules().

Upvotes: 0

tzortzik
tzortzik

Reputation: 5133

It seems that the problem was the timezone. Daylight saving time does not apply to Brazil/Acre. Here is another test which proves it works fine.

  @Test
  public void getDateAndTimeInUserFormat() throws Exception {
    I18NService service = prepareForUserFormatCheck("####.000", "Brazil/East");
    assertEquals(service.getDateAndTimeInUserFormat("2017-01-06T20:52:52+0200"),"06-01-2017 16:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-02-06T20:52:52+0200"),"06-02-2017 16:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-03-06T20:52:52+0200"),"06-03-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-04-06T20:52:52+0200"),"06-04-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-05-06T20:52:52+0200"),"06-05-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-06-06T20:52:52+0200"),"06-06-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-07-06T20:52:52+0200"),"06-07-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-08-06T20:52:52+0200"),"06-08-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-09-06T20:52:52+0200"),"06-09-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-10-06T20:52:52+0200"),"06-10-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-11-06T20:52:52+0200"),"06-11-2017 16:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-12-06T20:52:52+0200"),"06-12-2017 16:52");
  }

Upvotes: 1

Related Questions