From 226c49fa84a0d019f7cc0ec7a32f6f773484683a Mon Sep 17 00:00:00 2001 From: Vijay Palaniappan S Date: Tue, 21 Jan 2020 21:37:38 +0530 Subject: [PATCH] Added a new module & code for creating LocalDate (#8558) --- .../core-java-datetime-java8-2/pom.xml | 72 +++++++++++++++++++ .../baeldung/localdate/LocalDateExample.java | 31 ++++++++ .../localdate/LocalDateExampleUnitTest.java} | 33 +++------ .../com/baeldung/datebasics/CreateDate.java | 45 ------------ 4 files changed, 112 insertions(+), 69 deletions(-) create mode 100644 core-java-modules/core-java-datetime-java8-2/pom.xml create mode 100644 core-java-modules/core-java-datetime-java8-2/src/main/java/com/baeldung/localdate/LocalDateExample.java rename core-java-modules/{core-java-datetime-java8/src/test/java/com/baeldung/datebasics/CreateDateUnitTest.java => core-java-datetime-java8-2/src/test/java/com/baeldung/localdate/LocalDateExampleUnitTest.java} (65%) delete mode 100644 core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datebasics/CreateDate.java diff --git a/core-java-modules/core-java-datetime-java8-2/pom.xml b/core-java-modules/core-java-datetime-java8-2/pom.xml new file mode 100644 index 0000000000..34323fe76c --- /dev/null +++ b/core-java-modules/core-java-datetime-java8-2/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + core-java-datetime-java8 + ${project.parent.version} + core-java-datetime-java8 + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../../parent-java + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + joda-time + joda-time + ${joda-time.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + log4j + log4j + ${log4j.version} + test + + + + + core-java-datetime-java8 + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + 1.9 + 1.9 + 2.10 + + 3.6.1 + + + diff --git a/core-java-modules/core-java-datetime-java8-2/src/main/java/com/baeldung/localdate/LocalDateExample.java b/core-java-modules/core-java-datetime-java8-2/src/main/java/com/baeldung/localdate/LocalDateExample.java new file mode 100644 index 0000000000..f4c9e5431f --- /dev/null +++ b/core-java-modules/core-java-datetime-java8-2/src/main/java/com/baeldung/localdate/LocalDateExample.java @@ -0,0 +1,31 @@ +package com.baeldung.localdate; + +import java.time.LocalDate; +import java.time.Month; +import java.time.format.DateTimeFormatter; + +public class LocalDateExample { + public LocalDate getCustomDateOne(int year, int month, int dayOfMonth) { + return LocalDate.of(year, month, dayOfMonth); + } + + public LocalDate getCustomDateTwo(int year, Month month, int dayOfMonth) { + return LocalDate.of(year, month, dayOfMonth); + } + + public LocalDate getDateFromEpochDay(long epochDay) { + return LocalDate.ofEpochDay(epochDay); + } + + public LocalDate getDateFromYearAndDayOfYear(int year, int dayOfYear) { + return LocalDate.ofYearDay(year, dayOfYear); + } + + public LocalDate getDateFromString(String date) { + return LocalDate.parse(date); + } + + public LocalDate getDateFromStringAndFormatter(String date, String pattern) { + return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern)); + } +} diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datebasics/CreateDateUnitTest.java b/core-java-modules/core-java-datetime-java8-2/src/test/java/com/baeldung/localdate/LocalDateExampleUnitTest.java similarity index 65% rename from core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datebasics/CreateDateUnitTest.java rename to core-java-modules/core-java-datetime-java8-2/src/test/java/com/baeldung/localdate/LocalDateExampleUnitTest.java index 54f3285ea0..dff15486a4 100644 --- a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datebasics/CreateDateUnitTest.java +++ b/core-java-modules/core-java-datetime-java8-2/src/test/java/com/baeldung/localdate/LocalDateExampleUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.datebasics; +package com.baeldung.localdate; import static org.junit.Assert.assertEquals; @@ -6,49 +6,34 @@ import java.time.Month; import org.junit.Test; -public class CreateDateUnitTest { - private CreateDate date = new CreateDate(); - - @Test - public void whenUsingNowMethod_thenLocalDate() { - assertEquals("2020-01-08", date.getTodaysDate()); - } - - @Test - public void whenUsingClock_thenLocalDate() { - assertEquals("2020-01-08", date.getTodaysDateFromClock()); - } - - @Test - public void givenValues_whenUsingZone_thenLocalDate() { - assertEquals("2020-01-08", date.getTodaysDateFromZone("Asia/Kolkata")); - } - +public class LocalDateExampleUnitTest { + private LocalDateExample date = new LocalDateExample(); + @Test public void givenValues_whenUsingOfMethod_thenLocalDate() { assertEquals("2020-01-08", date.getCustomDateOne(2020, 1, 8)); } - + @Test public void givenValuesWithMonthEnum_whenUsingOfMethod_thenLocalDate() { assertEquals("2020-01-08", date.getCustomDateTwo(2020, Month.JANUARY, 8)); } - + @Test public void givenValues_whenUsingEpochDay_thenLocalDate() { assertEquals("2020-01-08", date.getDateFromEpochDay(18269)); } - + @Test public void givenValues_whenUsingYearDay_thenLocalDate() { assertEquals("2020-01-08", date.getDateFromYearAndDayOfYear(2020, 8)); } - + @Test public void givenValues_whenUsingParse_thenLocalDate() { assertEquals("2020-01-08", date.getDateFromString("2020-01-08")); } - + @Test public void givenValuesWithFormatter_whenUsingParse_thenLocalDate() { assertEquals("2020-01-08", date.getDateFromStringAndFormatter("8-Jan-2020", "d-MMM-yyyy")); diff --git a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datebasics/CreateDate.java b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datebasics/CreateDate.java deleted file mode 100644 index 327827e03a..0000000000 --- a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datebasics/CreateDate.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.baeldung.datebasics; - -import java.time.Clock; -import java.time.LocalDate; -import java.time.Month; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; - -public class CreateDate { - public LocalDate getTodaysDate() { - return LocalDate.now(); - } - - public LocalDate getTodaysDateFromClock() { - return LocalDate.now(Clock.systemDefaultZone()); - } - - public LocalDate getTodaysDateFromZone(String zone) { - return LocalDate.now(ZoneId.of(zone)); - } - - public LocalDate getCustomDateOne(int year, int month, int dayOfMonth) { - return LocalDate.of(year, month, dayOfMonth); - } - - public LocalDate getCustomDateTwo(int year, Month month, int dayOfMonth) { - return LocalDate.of(year, month, dayOfMonth); - } - - public LocalDate getDateFromEpochDay(long epochDay) { - return LocalDate.ofEpochDay(epochDay); - } - - public LocalDate getDateFromYearAndDayOfYear(int year, int dayOfYear) { - return LocalDate.ofYearDay(year, dayOfYear); - } - - public LocalDate getDateFromString(String date) { - return LocalDate.parse(date); - } - - public LocalDate getDateFromStringAndFormatter(String date, String pattern) { - return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern)); - } -}