BAEL-1599
This commit is contained in:
parent
b832ed0af8
commit
b07e50f46f
|
@ -25,20 +25,20 @@ public class DaylightSavingTimeExamplesTest {
|
|||
cal.setTime(dateBeforeDST);
|
||||
System.out.println("Before DST (00:55 UTC - 01:55 GMT+1) = " + dateBeforeDST);
|
||||
System.out.println("With this Calendar " + (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000) + " minutes must be added to UTC (GMT TimeZone) to get a correct date for this TimeZone\n");
|
||||
assertThat(cal.get(Calendar.ZONE_OFFSET)==3600000);
|
||||
assertThat(cal.get(Calendar.DST_OFFSET)==0);
|
||||
assertThat(cal.get(Calendar.ZONE_OFFSET)).isEqualTo(3600000);
|
||||
assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(0);
|
||||
|
||||
cal.add(Calendar.MINUTE, 10);
|
||||
|
||||
Date dateAfterDST = cal.getTime();
|
||||
System.out.println(" After DST (01:05 UTC - 03:05 GMT+2) = " + dateAfterDST);
|
||||
System.out.println("With this Calendar " + (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000) + " minutes must be added to UTC (GMT TimeZone) to get a correct date for this TimeZone\n");
|
||||
assertThat(cal.get(Calendar.DST_OFFSET)==3600000);
|
||||
assertThat(dateAfterDST == df.parse("2018-03-25 03:05"));
|
||||
assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(3600000);
|
||||
assertThat(dateAfterDST).isEqualTo(df.parse("2018-03-25 03:05"));
|
||||
|
||||
Long deltaBetweenDatesInMillis = dateAfterDST.getTime() - dateBeforeDST.getTime();
|
||||
Long seventyMinutesInMillis = (1000L * 60 * 70);
|
||||
assertThat(deltaBetweenDatesInMillis == seventyMinutesInMillis);
|
||||
Long tenMinutesInMillis = (1000L * 60 * 10);
|
||||
assertThat(deltaBetweenDatesInMillis).isEqualTo(tenMinutesInMillis);
|
||||
}
|
||||
|
||||
private void prettyPrint(TimeZone tz) {
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.dst;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DaylightSavingTimeJavaTimeExamplesTest {
|
||||
|
||||
@Test
|
||||
public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException {
|
||||
ZoneId italianZoneId = ZoneId.of("Europe/Rome");
|
||||
|
||||
LocalDateTime localDateTimeBeforeDST = LocalDateTime.of(2018, 3, 25, 1, 55);
|
||||
System.out.println(localDateTimeBeforeDST);
|
||||
assertThat(localDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55");
|
||||
|
||||
ZonedDateTime zonedDateTimeBeforeDST = localDateTimeBeforeDST.atZone(italianZoneId);
|
||||
prettyPrint(zonedDateTimeBeforeDST);
|
||||
assertThat(zonedDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55+01:00[Europe/Rome]");
|
||||
|
||||
ZonedDateTime zonedDateTimeAfterDST = zonedDateTimeBeforeDST.plus(10, ChronoUnit.MINUTES);
|
||||
prettyPrint(zonedDateTimeAfterDST);
|
||||
assertThat(zonedDateTimeAfterDST.toString()).isEqualTo("2018-03-25T03:05+02:00[Europe/Rome]");
|
||||
|
||||
Long deltaBetweenDatesInMinutes = ChronoUnit.MINUTES.between(zonedDateTimeBeforeDST,zonedDateTimeAfterDST);
|
||||
assertThat(deltaBetweenDatesInMinutes).isEqualTo(10);
|
||||
|
||||
}
|
||||
|
||||
private void prettyPrint(ZonedDateTime zdt) {
|
||||
System.out.println(String.format(
|
||||
" ZonedDateTime = %s\n"
|
||||
+ " Zone ID = %s (%s)\n"
|
||||
+ " RawOffset = %s minutes\n"
|
||||
+ " -----------------------------------------",
|
||||
zdt, zdt.getZone(), zdt.getZone().getId(), zdt.getOffset().getTotalSeconds()/60));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCounting_ThenPrintDifferencesBetweenAPIs() {
|
||||
System.out.println("Total java.time.ZoneId count : " + ZoneId.getAvailableZoneIds().size());
|
||||
System.out.println("Total java.util.TimeZone Id count : " + TimeZone.getAvailableIDs().length);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue