From 99ae59b9611c06045f376fc71e622687508d31b7 Mon Sep 17 00:00:00 2001 From: abialas Date: Fri, 17 Nov 2017 23:46:09 +0100 Subject: [PATCH] BAEL-1344 add java9 example --- .../datetime/DateToLocalDateConverter.java | 5 ++- .../DateToLocalDateTimeConverter.java | 5 ++- .../datetime/DateToLocalDateConverter.java | 22 ++++++++++ .../DateToLocalDateTimeConverter.java | 22 ++++++++++ .../DateToLocalDateConverterTest.java | 41 ++++++++++++++++++ .../DateToLocalDateTimeConverterTest.java | 43 +++++++++++++++++++ 6 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateConverter.java create mode 100644 core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverter.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateConverterTest.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverterTest.java 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 index 1ef77a2960..8788aac747 100644 --- a/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateConverter.java +++ b/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateConverter.java @@ -27,8 +27,9 @@ public class DateToLocalDateConverter { } public static LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) { - return LocalDate.from(Instant.ofEpochMilli(dateToConvert.getTime()) - .atZone(ZoneId.systemDefault())); + 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 index 78abb6a94c..f994023526 100644 --- a/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateTimeConverter.java +++ b/core-java-8/src/main/java/com/baeldung/datetime/DateToLocalDateTimeConverter.java @@ -27,8 +27,9 @@ public class DateToLocalDateTimeConverter { } public static LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) { - return LocalDateTime.from(Instant.ofEpochMilli(dateToConvert.getTime()) - .atZone(ZoneId.systemDefault())); + return Instant.ofEpochMilli(dateToConvert.getTime()) + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); } } 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 new file mode 100644 index 0000000000..bafa9ebff1 --- /dev/null +++ b/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateConverter.java @@ -0,0 +1,22 @@ +/** + * + */ +package com.baeldung.java9.datetime; + +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. + * + * @author abialas + * + */ +public class DateToLocalDateConverter { + + 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 new file mode 100644 index 0000000000..538d5a9f63 --- /dev/null +++ b/core-java-9/src/main/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverter.java @@ -0,0 +1,22 @@ +/** + * + */ +package com.baeldung.java9.datetime; + +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. + * + * @author abialas + * + */ +public class DateToLocalDateTimeConverter { + + public static LocalDateTime convertToLocalDateTime(Date dateToConvert) { + return LocalDateTime.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault()); + } + +} 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 new file mode 100644 index 0000000000..2e0fb0dedd --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateConverterTest.java @@ -0,0 +1,41 @@ +/** + * + */ +package com.baeldung.java9.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; + +import com.baeldung.java9.datetime.DateToLocalDateConverter; + +/** + * JUnits for {@link DateToLocalDateConverter} class. + * + * @author abialas + * + */ +public class DateToLocalDateConverterTest { + + @Test + public void shouldReturn10thNovember2010WhenConvertToLocalDate() { + // given + Calendar calendar = Calendar.getInstance(); + calendar.set(2010, 10, 10); + Date dateToConvert = calendar.getTime(); + + // when + LocalDate localDateTime = DateToLocalDateConverter.convertToLocalDate(dateToConvert); + + // then + assertEquals(2010, localDateTime.get(ChronoField.YEAR)); + assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); + assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); + } + +} 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 new file mode 100644 index 0000000000..49988c8b33 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/datetime/DateToLocalDateTimeConverterTest.java @@ -0,0 +1,43 @@ +/** + * + */ +package com.baeldung.java9.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; + +import com.baeldung.java9.datetime.DateToLocalDateTimeConverter; + +/** + * 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.convertToLocalDateTime(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)); + } + +}