From 068a21f32ad75396a27f7e6181f28ad8aa302f36 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 24 Mar 2018 10:17:26 +0200 Subject: [PATCH 1/7] formatting, dst error --- .../dst/DaylightSavingTimeExamplesTest.java | 81 +++++++++---------- ...aylightSavingTimeJavaTimeExamplesTest.java | 67 ++++++++------- 2 files changed, 73 insertions(+), 75 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java index 544f120b55..46f0af6fcd 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java @@ -14,47 +14,46 @@ import org.junit.Test; public class DaylightSavingTimeExamplesTest { - @Test - public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException { - TimeZone tz = TimeZone.getTimeZone("Europe/Rome"); - Calendar cal = Calendar.getInstance(tz, Locale.ITALIAN); - DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ITALIAN); - 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); + @Test + public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException { + 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.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); - assertThat(dateAfterDST).isEqualTo(df.parse("2018-03-25 03:05")); - - Long deltaBetweenDatesInMillis = dateAfterDST.getTime() - dateBeforeDST.getTime(); - Long tenMinutesInMillis = (1000L * 60 * 10); - assertThat(deltaBetweenDatesInMillis).isEqualTo(tenMinutesInMillis); - } + 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); - 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); - } - } + 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); + assertThat(dateAfterDST).isEqualTo(df.parse("2018-03-25 03:05")); + + Long deltaBetweenDatesInMillis = dateAfterDST.getTime() - dateBeforeDST.getTime(); + Long tenMinutesInMillis = (1000L * 60 * 10); + assertThat(deltaBetweenDatesInMillis).isEqualTo(tenMinutesInMillis); + } + + 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); + } + } } diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java index 9d68f928be..292267639f 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java @@ -13,40 +13,39 @@ 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); - - } + @Test + public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException { + ZoneId italianZoneId = ZoneId.of("Europe/Rome"); - 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); - } + 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); + } } From d12be45fe1c84500c8d8531ec4459beaf650f5a3 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 24 Mar 2018 10:22:40 +0200 Subject: [PATCH 2/7] formatting --- .../dst/DaylightSavingTimeExamplesTest.java | 15 +++++++++++---- .../DaylightSavingTimeJavaTimeExamplesTest.java | 12 ++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java index 46f0af6fcd..dbec873eb3 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java @@ -25,7 +25,7 @@ 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)).isEqualTo(3600000); assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(0); @@ -33,7 +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); @@ -45,8 +45,15 @@ 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)); + + //@formatter:off + 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)); + //@formatter:on } @Test diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java index 292267639f..15154416ae 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java @@ -35,10 +35,14 @@ public class DaylightSavingTimeJavaTimeExamplesTest { } 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)); + //@formatter:off + 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)); + //@formatter:on } @Test From 8e4094b84b29f60ad2fe54d3edcb33db50995618 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 24 Mar 2018 15:09:36 +0200 Subject: [PATCH 3/7] Update DaylightSavingTimeExamplesTest.java --- .../java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java index dbec873eb3..2eb8cfbcbf 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java @@ -48,7 +48,7 @@ public class DaylightSavingTimeExamplesTest { //@formatter:off System.out.println(String.format( - " Zone ID = %s (%s)\n" + " Zone ID = %s (%s)\n" + " RawOffset = %s minutes\n" + " DST = %s minutes\n" + " -----------------------------------------", From 72b2e5e984217b5db1f65c33a62b3757db14965a Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 24 Mar 2018 17:30:49 +0200 Subject: [PATCH 4/7] Update DaylightSavingTimeJavaTimeExamplesTest.java --- .../baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java index 15154416ae..42177cfbb2 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java @@ -37,7 +37,7 @@ public class DaylightSavingTimeJavaTimeExamplesTest { private void prettyPrint(ZonedDateTime zdt) { //@formatter:off System.out.println(String.format( - " ZonedDateTime = %s\n" + " ZonedDateTime = %s\n" + " Zone ID = %s (%s)\n" + " RawOffset = %s minutes\n" + " -----------------------------------------", From 8182d49cf2886a09bab7a0fab1d013befe6952cd Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 24 Mar 2018 20:16:49 +0200 Subject: [PATCH 5/7] Update DaylightSavingTimeJavaTimeExamplesTest.java --- .../com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java index 42177cfbb2..78847033ac 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java @@ -52,4 +52,5 @@ public class DaylightSavingTimeJavaTimeExamplesTest { System.out.println("Total java.util.TimeZone Id count : " + TimeZone.getAvailableIDs().length); } + } From 98010d02e4963a59049b1f98820a36bf80382cb3 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 24 Mar 2018 21:27:02 +0200 Subject: [PATCH 6/7] set default timezone --- .../com/baeldung/dst/DaylightSavingTimeExamplesTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java index dbec873eb3..89764d5f84 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java @@ -15,11 +15,12 @@ import org.junit.Test; public class DaylightSavingTimeExamplesTest { @Test - public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException { + public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException { + TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome")); + 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()); From 7a77e32fe67bd789316e690049895bf05f1740c7 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 24 Mar 2018 21:34:44 +0200 Subject: [PATCH 7/7] ignore long test --- .../java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java index 51c9d48c97..8fec58f111 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java @@ -10,6 +10,7 @@ import java.util.Date; import java.util.Locale; import java.util.TimeZone; +import org.junit.Ignore; import org.junit.Test; public class DaylightSavingTimeExamplesTest { @@ -58,6 +59,7 @@ public class DaylightSavingTimeExamplesTest { } @Test + @Ignore public void whenIterating_ThenPrintAllTimeZones() { for (String id : TimeZone.getAvailableIDs()) { TimeZone tz = TimeZone.getTimeZone(id);