Added a new module & code for creating LocalDate (#8558)
This commit is contained in:
parent
24e69ed49a
commit
226c49fa84
|
@ -0,0 +1,72 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-datetime-java8</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<name>core-java-datetime-java8</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-datetime-java8</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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"));
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue