From b63caab3976ce6c8abed41d3bec15183e24e8c58 Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 17 Feb 2018 18:30:36 +0100 Subject: [PATCH 1/3] . --- .../DecimalFormatExamplesTest.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java diff --git a/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java b/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java new file mode 100644 index 0000000000..8acd4e023e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java @@ -0,0 +1,116 @@ +package com.baeldung.decimalformat; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.Locale; + +import org.junit.Test; + +public class DecimalFormatExamplesTest { + + double d = 1234567.89; + + @Test + public void givenSimpleDecimal_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("#########.###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("000000000.000", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("001234567.890"); + + } + + @Test + public void givenSmallerDecimalPattern_WhenFormatting_ThenRounding() { + + assertThat(new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234567.9"); + + assertThat(new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234568"); + + } + + @Test + public void givenGroupingSeparator_WhenFormatting_ThenGroupedOutput() { + + assertThat(new DecimalFormat("#,###.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,567.9"); + + assertThat(new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,568"); + + } + + @Test + public void givenMixedPattern_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("The # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("The 1234568 number"); + + assertThat(new DecimalFormat("The '#' # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("The # 1234568 number"); + + } + + @Test + public void givenLocales_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,567.89"); + + assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ITALIAN)).format(d)) + .isEqualTo("1.234.567,89"); + + assertThat(new DecimalFormat("#,###.##", DecimalFormatSymbols.getInstance(new Locale("it", "IT"))).format(d)) + .isEqualTo("1.234.567,89"); + + } + + @Test + public void givenE_WhenFormatting_ThenScientificNotation() { + + assertThat(new DecimalFormat("00.#######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("12.3456789E5"); + + assertThat(new DecimalFormat("000.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("123.456789E4"); + + assertThat(new DecimalFormat("##0.######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1.23456789E6"); + + assertThat(new DecimalFormat("###.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1.23456789E6"); + + } + + @Test + public void givenString_WhenParsing_ThenCorrectOutput() throws ParseException { + + assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)).parse("1234567.89")) + .isEqualTo(1234567.89); + + assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ITALIAN)).parse("1.234.567,89")) + .isEqualTo(1234567.89); + + } + + @Test + public void givenStringAndBigDecimalFlag_WhenParsing_ThenCorrectOutput() throws ParseException { + + NumberFormat nf = new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)); + ((DecimalFormat) nf).setParseBigDecimal(true); + assertThat(nf.parse("1234567.89")).isEqualTo(BigDecimal.valueOf(1234567.89)); + } + +} From c5ac4b4bba0e6bbcacc4dd3a7fccc5851a67f00d Mon Sep 17 00:00:00 2001 From: andrea Date: Sun, 11 Mar 2018 21:35:00 +0100 Subject: [PATCH 2/3] BAEL-1599 --- .../dst/DaylightSavingTimeExamplesTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java new file mode 100644 index 0000000000..9fa183d873 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java @@ -0,0 +1,60 @@ +package com.baeldung.dst; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +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)==3600000); + assertThat(cal.get(Calendar.DST_OFFSET)==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")); + + Long deltaBetweenDatesInMillis = dateAfterDST.getTime() - dateBeforeDST.getTime(); + Long seventyMinutesInMillis = (1000L * 60 * 70); + assertThat(deltaBetweenDatesInMillis == seventyMinutesInMillis); + } + + 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); + } + } +} From b07e50f46f4596ab2954ed3bacccb38bfd23b7e4 Mon Sep 17 00:00:00 2001 From: andrea Date: Fri, 23 Mar 2018 01:21:24 +0100 Subject: [PATCH 3/3] BAEL-1599 --- .../dst/DaylightSavingTimeExamplesTest.java | 12 ++--- ...aylightSavingTimeJavaTimeExamplesTest.java | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java 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 9fa183d873..544f120b55 100644 --- a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeExamplesTest.java @@ -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) { diff --git a/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java new file mode 100644 index 0000000000..9d68f928be --- /dev/null +++ b/core-java/src/test/java/com/baeldung/dst/DaylightSavingTimeJavaTimeExamplesTest.java @@ -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); + } + +}