formatting, dst error

This commit is contained in:
Loredana Crusoveanu 2018-03-24 10:17:26 +02:00
parent 86e8493bbf
commit 068a21f32a
2 changed files with 73 additions and 75 deletions

View File

@ -19,11 +19,13 @@ public class DaylightSavingTimeExamplesTest {
TimeZone tz = TimeZone.getTimeZone("Europe/Rome");
Calendar cal = Calendar.getInstance(tz, Locale.ITALIAN);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ITALIAN);
df.setTimeZone(tz);
Date dateBeforeDST = df.parse("2018-03-25 01:55");
prettyPrint(cal.getTimeZone());
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)).isEqualTo(3600000);
assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(0);
@ -31,6 +33,7 @@ public class DaylightSavingTimeExamplesTest {
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)).isEqualTo(3600000);
@ -42,12 +45,8 @@ public class DaylightSavingTimeExamplesTest {
}
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));
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

View File

@ -29,23 +29,22 @@ public class DaylightSavingTimeJavaTimeExamplesTest {
prettyPrint(zonedDateTimeAfterDST);
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);
}
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));
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.time.ZoneId count : " + ZoneId.getAvailableZoneIds()
.size());
System.out.println("Total java.util.TimeZone Id count : " + TimeZone.getAvailableIDs().length);
}