From edec7ddbe51d7b5e51dcd4fafb33a1599b02ed14 Mon Sep 17 00:00:00 2001 From: abialas Date: Fri, 24 Nov 2017 22:29:08 +0100 Subject: [PATCH] BAEL-1344 (#3081) * BAEL-1344 add java8 convert methods date, localdate, localdatetime * BAEL-1344 add java9 example * Move Date to LocalDate and LocalDateTime converters from core-java-8 to core-java-9 --- .../datetime/DateToLocalDateConverter.java | 35 --------- .../DateToLocalDateTimeConverter.java | 35 --------- .../DateToLocalDateConverterTest.java | 72 ----------------- .../DateToLocalDateTimeConverterTest.java | 78 ------------------- .../datetime/DateToLocalDateConverter.java | 19 ++++- .../DateToLocalDateTimeConverter.java | 19 ++++- .../LocalDateTimeToDateConverter.java | 2 +- .../datetime/LocalDateToDateConverter.java | 2 +- .../DateToLocalDateConverterTest.java | 48 ++++++++++++ .../DateToLocalDateTimeConverterTest.java | 54 +++++++++++++ .../LocalDateTimeToDateConverterTest.java | 2 +- .../LocalDateToDateConverterTest.java | 2 +- 12 files changed, 142 insertions(+), 226 deletions(-) delete mode 100644 core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateConverter.java delete mode 100644 core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateTimeConverter.java delete mode 100644 core-java-8/src/test/java/com/baeldung/datetime/DateToLocalDateConverterTest.java delete mode 100644 core-java-8/src/test/java/com/baeldung/datetime/DateToLocalDateTimeConverterTest.java rename {core-java-8/src/main/java/com/baeldung => core-java-9/src/main/java/com/baeldung/java9}/datetime/LocalDateTimeToDateConverter.java (94%) rename {core-java-8/src/main/java/com/baeldung => core-java-9/src/main/java/com/baeldung/java9}/datetime/LocalDateToDateConverter.java (94%) rename {core-java-8/src/test/java/com/baeldung => core-java-9/src/test/java/com/baeldung/java9}/datetime/LocalDateTimeToDateConverterTest.java (97%) rename {core-java-8/src/test/java/com/baeldung => core-java-9/src/test/java/com/baeldung/java9}/datetime/LocalDateToDateConverterTest.java (97%) diff --git a/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateConverter.java b/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateConverter.java deleted file mode 100644 index 8788aac747..0000000000 --- a/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateConverter.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * - */ -package com.baeldung.datetime; - -import java.time.Instant; -import java.time.LocalDate; -import java.time.ZoneId; -import java.util.Date; - -/** - * Class which shows different ways of converting java.util.Date into java.time.LocalDate. - * - * @author abialas - * - */ -public class DateToLocalDateConverter { - - public static LocalDate convertToLocalDateViaInstant(Date dateToConvert) { - return dateToConvert.toInstant() - .atZone(ZoneId.systemDefault()) - .toLocalDate(); - } - - public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) { - return new java.sql.Date(dateToConvert.getTime()).toLocalDate(); - } - - public static LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) { - return Instant.ofEpochMilli(dateToConvert.getTime()) - .atZone(ZoneId.systemDefault()) - .toLocalDate(); - } - -} diff --git a/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateTimeConverter.java b/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateTimeConverter.java deleted file mode 100644 index f994023526..0000000000 --- a/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateTimeConverter.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * - */ -package com.baeldung.datetime; - -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.util.Date; - -/** - * Class which shows different ways of converting java.util.Date into java.time.LocalDateTime. - * - * @author abialas - * - */ -public class DateToLocalDateTimeConverter { - - public static LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) { - return dateToConvert.toInstant() - .atZone(ZoneId.systemDefault()) - .toLocalDateTime(); - } - - public static LocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) { - return new java.sql.Timestamp(dateToConvert.getTime()).toLocalDateTime(); - } - - public static LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) { - return Instant.ofEpochMilli(dateToConvert.getTime()) - .atZone(ZoneId.systemDefault()) - .toLocalDateTime(); - } - -} diff --git a/core-java-8/src/test/java/com/baeldung/datetime/DateToLocalDateConverterTest.java b/core-java-8/src/test/java/com/baeldung/datetime/DateToLocalDateConverterTest.java deleted file mode 100644 index 5de6ae3e59..0000000000 --- a/core-java-8/src/test/java/com/baeldung/datetime/DateToLocalDateConverterTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * - */ -package com.baeldung.datetime; - -import static org.junit.Assert.assertEquals; - -import java.time.LocalDate; -import java.time.temporal.ChronoField; -import java.util.Calendar; -import java.util.Date; - -import org.junit.Test; - -/** - * - * JUnits for {@link DateToLocalDateConverter} class. - * - * @author abialas - * - */ -public class DateToLocalDateConverterTest { - - @Test - public void shouldReturn10thNovember2010WhenConvertViaInstant() { - // given - Calendar calendar = Calendar.getInstance(); - calendar.set(2010, 10, 10); - Date dateToConvert = calendar.getTime(); - - // when - LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaInstant(dateToConvert); - - // then - assertEquals(2010, localDate.get(ChronoField.YEAR)); - assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); - assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); - } - - @Test - public void shouldReturn10thNovember2010WhenConvertViaMiliseconds() { - // given - Calendar calendar = Calendar.getInstance(); - calendar.set(2010, 10, 10); - Date dateToConvert = calendar.getTime(); - - // when - LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaMilisecond(dateToConvert); - - // then - assertEquals(2010, localDate.get(ChronoField.YEAR)); - assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); - assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); - } - - @Test - public void shouldReturn10thNovember2010WhenConvertViaSqlDate() { - // given - Calendar calendar = Calendar.getInstance(); - calendar.set(2010, 10, 10); - Date dateToConvert = calendar.getTime(); - - // when - LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaSqlDate(dateToConvert); - - // then - assertEquals(2010, localDate.get(ChronoField.YEAR)); - assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); - assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); - } - -} diff --git a/core-java-8/src/test/java/com/baeldung/datetime/DateToLocalDateTimeConverterTest.java b/core-java-8/src/test/java/com/baeldung/datetime/DateToLocalDateTimeConverterTest.java deleted file mode 100644 index 6d8fb8ea93..0000000000 --- a/core-java-8/src/test/java/com/baeldung/datetime/DateToLocalDateTimeConverterTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/** - * - */ -package com.baeldung.datetime; - -import static org.junit.Assert.assertEquals; - -import java.time.LocalDateTime; -import java.time.temporal.ChronoField; -import java.util.Calendar; -import java.util.Date; - -import org.junit.Test; - -/** - * - * JUnits for {@link DateToLocalDateTimeConverter} class. - * - * @author abialas - * - */ -public class DateToLocalDateTimeConverterTest { - - @Test - public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() { - // given - Calendar calendar = Calendar.getInstance(); - calendar.set(2010, 10, 10, 8, 20); - Date dateToConvert = calendar.getTime(); - - // when - LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaInstant(dateToConvert); - - // then - assertEquals(2010, localDateTime.get(ChronoField.YEAR)); - assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); - assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); - assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); - assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); - } - - @Test - public void shouldReturn10thNovember2010time8hour20minWhenConvertViaMiliseconds() { - // given - Calendar calendar = Calendar.getInstance(); - calendar.set(2010, 10, 10, 8, 20); - Date dateToConvert = calendar.getTime(); - - // when - LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaMilisecond(dateToConvert); - - // then - assertEquals(2010, localDateTime.get(ChronoField.YEAR)); - assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); - assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); - assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); - assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); - } - - @Test - public void shouldReturn10thNovember2010time8hour20minWhenConvertViaSqlTimestamp() { - // given - Calendar calendar = Calendar.getInstance(); - calendar.set(2010, 10, 10, 8, 20); - Date dateToConvert = calendar.getTime(); - - // when - LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaSqlTimestamp(dateToConvert); - - // then - assertEquals(2010, localDateTime.get(ChronoField.YEAR)); - assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); - assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); - assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); - assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); - } - -} diff --git a/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateConverter.java b/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateConverter.java index bafa9ebff1..c794c57e87 100644 --- a/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateConverter.java +++ b/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateConverter.java @@ -3,18 +3,35 @@ */ package com.baeldung.java9.datetime; +import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.util.Date; /** - * Class which shows a way to convert java.util.Date into java.time.LocalDate with new Java 1.9. + * Class which shows a way to convert java.util.Date into java.time.LocalDate. * * @author abialas * */ public class DateToLocalDateConverter { + public static LocalDate convertToLocalDateViaInstant(Date dateToConvert) { + return dateToConvert.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + } + + public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) { + return new java.sql.Date(dateToConvert.getTime()).toLocalDate(); + } + + public static LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) { + return Instant.ofEpochMilli(dateToConvert.getTime()) + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + } + public static LocalDate convertToLocalDate(Date dateToConvert) { return LocalDate.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault()); } diff --git a/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverter.java b/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverter.java index 538d5a9f63..17ca5b1122 100644 --- a/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverter.java +++ b/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverter.java @@ -3,18 +3,35 @@ */ package com.baeldung.java9.datetime; +import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; /** - * Class which shows a way to convert java.util.Date into java.time.LocalDateTime with new Java 1.9. + * Class which shows a way to convert java.util.Date into java.time.LocalDateTime. * * @author abialas * */ public class DateToLocalDateTimeConverter { + public static LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) { + return dateToConvert.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + } + + public static LocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) { + return new java.sql.Timestamp(dateToConvert.getTime()).toLocalDateTime(); + } + + public static LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) { + return Instant.ofEpochMilli(dateToConvert.getTime()) + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + } + public static LocalDateTime convertToLocalDateTime(Date dateToConvert) { return LocalDateTime.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault()); } diff --git a/core-java-8/src/main/java/com/baeldung/datetime/LocalDateTimeToDateConverter.java b/core-java-9/src/main/java/com/baeldung/java9/datetime/LocalDateTimeToDateConverter.java similarity index 94% rename from core-java-8/src/main/java/com/baeldung/datetime/LocalDateTimeToDateConverter.java rename to core-java-9/src/main/java/com/baeldung/java9/datetime/LocalDateTimeToDateConverter.java index ef72c8b4fb..f219dcf038 100644 --- a/core-java-8/src/main/java/com/baeldung/datetime/LocalDateTimeToDateConverter.java +++ b/core-java-9/src/main/java/com/baeldung/java9/datetime/LocalDateTimeToDateConverter.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.datetime; +package com.baeldung.java9.datetime; import java.time.LocalDateTime; import java.time.ZoneId; diff --git a/core-java-8/src/main/java/com/baeldung/datetime/LocalDateToDateConverter.java b/core-java-9/src/main/java/com/baeldung/java9/datetime/LocalDateToDateConverter.java similarity index 94% rename from core-java-8/src/main/java/com/baeldung/datetime/LocalDateToDateConverter.java rename to core-java-9/src/main/java/com/baeldung/java9/datetime/LocalDateToDateConverter.java index 8050815799..f9893da5d0 100644 --- a/core-java-8/src/main/java/com/baeldung/datetime/LocalDateToDateConverter.java +++ b/core-java-9/src/main/java/com/baeldung/java9/datetime/LocalDateToDateConverter.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.datetime; +package com.baeldung.java9.datetime; import java.time.LocalDate; import java.time.ZoneId; diff --git a/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateConverterTest.java b/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateConverterTest.java index 2e0fb0dedd..ab69bba359 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateConverterTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateConverterTest.java @@ -22,6 +22,54 @@ import com.baeldung.java9.datetime.DateToLocalDateConverter; */ public class DateToLocalDateConverterTest { + @Test + public void shouldReturn10thNovember2010WhenConvertViaInstant() { + // given + Calendar calendar = Calendar.getInstance(); + calendar.set(2010, 10, 10); + Date dateToConvert = calendar.getTime(); + + // when + LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaInstant(dateToConvert); + + // then + assertEquals(2010, localDate.get(ChronoField.YEAR)); + assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); + assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); + } + + @Test + public void shouldReturn10thNovember2010WhenConvertViaMiliseconds() { + // given + Calendar calendar = Calendar.getInstance(); + calendar.set(2010, 10, 10); + Date dateToConvert = calendar.getTime(); + + // when + LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaMilisecond(dateToConvert); + + // then + assertEquals(2010, localDate.get(ChronoField.YEAR)); + assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); + assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); + } + + @Test + public void shouldReturn10thNovember2010WhenConvertViaSqlDate() { + // given + Calendar calendar = Calendar.getInstance(); + calendar.set(2010, 10, 10); + Date dateToConvert = calendar.getTime(); + + // when + LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaSqlDate(dateToConvert); + + // then + assertEquals(2010, localDate.get(ChronoField.YEAR)); + assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); + assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); + } + @Test public void shouldReturn10thNovember2010WhenConvertToLocalDate() { // given diff --git a/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverterTest.java b/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverterTest.java index 49988c8b33..97c70ee5ac 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverterTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverterTest.java @@ -29,6 +29,60 @@ public class DateToLocalDateTimeConverterTest { calendar.set(2010, 10, 10, 8, 20); Date dateToConvert = calendar.getTime(); + // when + LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaInstant(dateToConvert); + + // then + assertEquals(2010, localDateTime.get(ChronoField.YEAR)); + assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); + assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); + assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); + assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); + } + + @Test + public void shouldReturn10thNovember2010time8hour20minWhenConvertViaMiliseconds() { + // given + Calendar calendar = Calendar.getInstance(); + calendar.set(2010, 10, 10, 8, 20); + Date dateToConvert = calendar.getTime(); + + // when + LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaMilisecond(dateToConvert); + + // then + assertEquals(2010, localDateTime.get(ChronoField.YEAR)); + assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); + assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); + assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); + assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); + } + + @Test + public void shouldReturn10thNovember2010time8hour20minWhenConvertViaSqlTimestamp() { + // given + Calendar calendar = Calendar.getInstance(); + calendar.set(2010, 10, 10, 8, 20); + Date dateToConvert = calendar.getTime(); + + // when + LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaSqlTimestamp(dateToConvert); + + // then + assertEquals(2010, localDateTime.get(ChronoField.YEAR)); + assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); + assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); + assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); + assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); + } + + @Test + public void shouldReturn10thNovember2010time8hour20minWhenConvertToLocalDateTime() { + // given + Calendar calendar = Calendar.getInstance(); + calendar.set(2010, 10, 10, 8, 20); + Date dateToConvert = calendar.getTime(); + // when LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTime(dateToConvert); diff --git a/core-java-8/src/test/java/com/baeldung/datetime/LocalDateTimeToDateConverterTest.java b/core-java-9/src/test/java/com/baeldung/java9/datetime/LocalDateTimeToDateConverterTest.java similarity index 97% rename from core-java-8/src/test/java/com/baeldung/datetime/LocalDateTimeToDateConverterTest.java rename to core-java-9/src/test/java/com/baeldung/java9/datetime/LocalDateTimeToDateConverterTest.java index 519fa69f04..2c6898381f 100644 --- a/core-java-8/src/test/java/com/baeldung/datetime/LocalDateTimeToDateConverterTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/datetime/LocalDateTimeToDateConverterTest.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.datetime; +package com.baeldung.java9.datetime; import static org.junit.Assert.assertEquals; diff --git a/core-java-8/src/test/java/com/baeldung/datetime/LocalDateToDateConverterTest.java b/core-java-9/src/test/java/com/baeldung/java9/datetime/LocalDateToDateConverterTest.java similarity index 97% rename from core-java-8/src/test/java/com/baeldung/datetime/LocalDateToDateConverterTest.java rename to core-java-9/src/test/java/com/baeldung/java9/datetime/LocalDateToDateConverterTest.java index c1da3af052..7f20d5d2d2 100644 --- a/core-java-8/src/test/java/com/baeldung/datetime/LocalDateToDateConverterTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/datetime/LocalDateToDateConverterTest.java @@ -1,7 +1,7 @@ /** * */ -package com.baeldung.datetime; +package com.baeldung.java9.datetime; import static org.junit.Assert.assertEquals;