formatting, dst error
This commit is contained in:
parent
86e8493bbf
commit
068a21f32a
@ -14,47 +14,46 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class DaylightSavingTimeExamplesTest {
|
public class DaylightSavingTimeExamplesTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException {
|
public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException {
|
||||||
TimeZone tz = TimeZone.getTimeZone("Europe/Rome");
|
TimeZone tz = TimeZone.getTimeZone("Europe/Rome");
|
||||||
Calendar cal = Calendar.getInstance(tz, Locale.ITALIAN);
|
Calendar cal = Calendar.getInstance(tz, Locale.ITALIAN);
|
||||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ITALIAN);
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ITALIAN);
|
||||||
Date dateBeforeDST = df.parse("2018-03-25 01:55");
|
df.setTimeZone(tz);
|
||||||
prettyPrint(cal.getTimeZone());
|
Date dateBeforeDST = df.parse("2018-03-25 01:55");
|
||||||
|
prettyPrint(cal.getTimeZone());
|
||||||
|
|
||||||
cal.setTime(dateBeforeDST);
|
cal.setTime(dateBeforeDST);
|
||||||
System.out.println("Before DST (00:55 UTC - 01:55 GMT+1) = " + 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)).isEqualTo(3600000);
|
|
||||||
assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(0);
|
|
||||||
|
|
||||||
cal.add(Calendar.MINUTE, 10);
|
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)).isEqualTo(3600000);
|
||||||
|
assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(0);
|
||||||
|
|
||||||
Date dateAfterDST = cal.getTime();
|
cal.add(Calendar.MINUTE, 10);
|
||||||
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)).isEqualTo(3600000);
|
|
||||||
assertThat(dateAfterDST).isEqualTo(df.parse("2018-03-25 03:05"));
|
|
||||||
|
|
||||||
Long deltaBetweenDatesInMillis = dateAfterDST.getTime() - dateBeforeDST.getTime();
|
Date dateAfterDST = cal.getTime();
|
||||||
Long tenMinutesInMillis = (1000L * 60 * 10);
|
|
||||||
assertThat(deltaBetweenDatesInMillis).isEqualTo(tenMinutesInMillis);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void prettyPrint(TimeZone tz) {
|
System.out.println(" After DST (01:05 UTC - 03:05 GMT+2) = " + dateAfterDST);
|
||||||
System.out.println(String.format(
|
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");
|
||||||
" Zone ID = %s (%s)\n"
|
assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(3600000);
|
||||||
+ " RawOffset = %s minutes\n"
|
assertThat(dateAfterDST).isEqualTo(df.parse("2018-03-25 03:05"));
|
||||||
+ " DST = %s minutes\n"
|
|
||||||
+ " -----------------------------------------",
|
|
||||||
tz.getID(), tz.getDisplayName(), tz.getRawOffset()/60000, tz.getDSTSavings()/60000));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
Long deltaBetweenDatesInMillis = dateAfterDST.getTime() - dateBeforeDST.getTime();
|
||||||
public void whenIterating_ThenPrintAllTimeZones() {
|
Long tenMinutesInMillis = (1000L * 60 * 10);
|
||||||
for (String id : TimeZone.getAvailableIDs()) {
|
assertThat(deltaBetweenDatesInMillis).isEqualTo(tenMinutesInMillis);
|
||||||
TimeZone tz = TimeZone.getTimeZone(id);
|
}
|
||||||
prettyPrint(tz);
|
|
||||||
}
|
private void prettyPrint(TimeZone tz) {
|
||||||
}
|
System.out.println(
|
||||||
|
String.format(" Zone ID = %s (%s)\n" + " RawOffset = %s minutes\n" + " DST = %s minutes\n" + " -----------------------------------------", tz.getID(), tz.getDisplayName(), tz.getRawOffset() / 60000, tz.getDSTSavings() / 60000));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenIterating_ThenPrintAllTimeZones() {
|
||||||
|
for (String id : TimeZone.getAvailableIDs()) {
|
||||||
|
TimeZone tz = TimeZone.getTimeZone(id);
|
||||||
|
prettyPrint(tz);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,40 +13,39 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class DaylightSavingTimeJavaTimeExamplesTest {
|
public class DaylightSavingTimeJavaTimeExamplesTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException {
|
public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException {
|
||||||
ZoneId italianZoneId = ZoneId.of("Europe/Rome");
|
ZoneId italianZoneId = ZoneId.of("Europe/Rome");
|
||||||
|
|
||||||
LocalDateTime localDateTimeBeforeDST = LocalDateTime.of(2018, 3, 25, 1, 55);
|
LocalDateTime localDateTimeBeforeDST = LocalDateTime.of(2018, 3, 25, 1, 55);
|
||||||
System.out.println(localDateTimeBeforeDST);
|
System.out.println(localDateTimeBeforeDST);
|
||||||
assertThat(localDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55");
|
assertThat(localDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55");
|
||||||
|
|
||||||
ZonedDateTime zonedDateTimeBeforeDST = localDateTimeBeforeDST.atZone(italianZoneId);
|
ZonedDateTime zonedDateTimeBeforeDST = localDateTimeBeforeDST.atZone(italianZoneId);
|
||||||
prettyPrint(zonedDateTimeBeforeDST);
|
prettyPrint(zonedDateTimeBeforeDST);
|
||||||
assertThat(zonedDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55+01:00[Europe/Rome]");
|
assertThat(zonedDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55+01:00[Europe/Rome]");
|
||||||
|
|
||||||
ZonedDateTime zonedDateTimeAfterDST = zonedDateTimeBeforeDST.plus(10, ChronoUnit.MINUTES);
|
ZonedDateTime zonedDateTimeAfterDST = zonedDateTimeBeforeDST.plus(10, ChronoUnit.MINUTES);
|
||||||
prettyPrint(zonedDateTimeAfterDST);
|
prettyPrint(zonedDateTimeAfterDST);
|
||||||
assertThat(zonedDateTimeAfterDST.toString()).isEqualTo("2018-03-25T03:05+02:00[Europe/Rome]");
|
assertThat(zonedDateTimeAfterDST.toString()).isEqualTo("2018-03-25T03:05+02:00[Europe/Rome]");
|
||||||
|
|
||||||
Long deltaBetweenDatesInMinutes = ChronoUnit.MINUTES.between(zonedDateTimeBeforeDST,zonedDateTimeAfterDST);
|
Long deltaBetweenDatesInMinutes = ChronoUnit.MINUTES.between(zonedDateTimeBeforeDST, zonedDateTimeAfterDST);
|
||||||
assertThat(deltaBetweenDatesInMinutes).isEqualTo(10);
|
assertThat(deltaBetweenDatesInMinutes).isEqualTo(10);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prettyPrint(ZonedDateTime zdt) {
|
private void prettyPrint(ZonedDateTime zdt) {
|
||||||
System.out.println(String.format(
|
System.out.println(String.format(" ZonedDateTime = %s\n" + " Zone ID = %s (%s)\n" + " RawOffset = %s minutes\n" + " -----------------------------------------", zdt, zdt.getZone(), zdt.getZone()
|
||||||
" ZonedDateTime = %s\n"
|
.getId(),
|
||||||
+ " Zone ID = %s (%s)\n"
|
zdt.getOffset()
|
||||||
+ " RawOffset = %s minutes\n"
|
.getTotalSeconds() / 60));
|
||||||
+ " -----------------------------------------",
|
}
|
||||||
zdt, zdt.getZone(), zdt.getZone().getId(), zdt.getOffset().getTotalSeconds()/60));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCounting_ThenPrintDifferencesBetweenAPIs() {
|
public void whenCounting_ThenPrintDifferencesBetweenAPIs() {
|
||||||
System.out.println("Total java.time.ZoneId count : " + ZoneId.getAvailableZoneIds().size());
|
System.out.println("Total java.time.ZoneId count : " + ZoneId.getAvailableZoneIds()
|
||||||
System.out.println("Total java.util.TimeZone Id count : " + TimeZone.getAvailableIDs().length);
|
.size());
|
||||||
}
|
System.out.println("Total java.util.TimeZone Id count : " + TimeZone.getAvailableIDs().length);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user