JAVA-617 Rename core-java-datetime-java8 to core-java-8-datetime
This commit is contained in:
5
core-java-modules/core-java-8-datetime-2/README.md
Normal file
5
core-java-modules/core-java-8-datetime-2/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Generating Random Dates in Java](https://www.baeldung.com/java-random-dates)
|
||||
- [Creating a LocalDate with Values in Java](https://www.baeldung.com/java-creating-localdate-with-values)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1)
|
||||
74
core-java-modules/core-java-8-datetime-2/pom.xml
Normal file
74
core-java-modules/core-java-8-datetime-2/pom.xml
Normal file
@@ -0,0 +1,74 @@
|
||||
<?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-8-datetime</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<name>core-java-8-datetime</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.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class LegacyRandomDateTimes {
|
||||
|
||||
public static Date between(Date startInclusive, Date endExclusive) {
|
||||
long startMillis = startInclusive.getTime();
|
||||
long endMillis = endExclusive.getTime();
|
||||
long randomMillisSinceEpoch = ThreadLocalRandom.current().nextLong(startMillis, endMillis);
|
||||
|
||||
return new Date(randomMillisSinceEpoch);
|
||||
}
|
||||
|
||||
public static Date timestamp() {
|
||||
return new Date(ThreadLocalRandom.current().nextInt() * 1000L);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class RandomDateTimes {
|
||||
|
||||
public static Instant timestamp() {
|
||||
return Instant.ofEpochSecond(ThreadLocalRandom.current().nextInt());
|
||||
}
|
||||
|
||||
public static Instant between(Instant startInclusive, Instant endExclusive) {
|
||||
long startSeconds = startInclusive.getEpochSecond();
|
||||
long endSeconds = endExclusive.getEpochSecond();
|
||||
long random = ThreadLocalRandom.current().nextLong(startSeconds, endSeconds);
|
||||
|
||||
return Instant.ofEpochSecond(random);
|
||||
}
|
||||
|
||||
public static Instant after(Instant startInclusive) {
|
||||
return between(startInclusive, Instant.MAX);
|
||||
}
|
||||
|
||||
public static Instant before(Instant upperExclusive) {
|
||||
return between(Instant.MIN, upperExclusive);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class RandomDates {
|
||||
|
||||
public static LocalDate between(LocalDate startInclusive, LocalDate endExclusive) {
|
||||
long startEpochDay = startInclusive.toEpochDay();
|
||||
long endEpochDay = endExclusive.toEpochDay();
|
||||
long randomDay = ThreadLocalRandom.current().nextLong(startEpochDay, endEpochDay);
|
||||
|
||||
return LocalDate.ofEpochDay(randomDay);
|
||||
}
|
||||
|
||||
public static LocalDate date() {
|
||||
int hundredYears = 100 * 365;
|
||||
return LocalDate.ofEpochDay(ThreadLocalRandom.current().nextInt(-hundredYears, hundredYears));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class RandomTimes {
|
||||
|
||||
public static LocalTime between(LocalTime startTime, LocalTime endTime) {
|
||||
int startSeconds = startTime.toSecondOfDay();
|
||||
int endSeconds = endTime.toSecondOfDay();
|
||||
int randomTime = ThreadLocalRandom.current().nextInt(startSeconds, endSeconds);
|
||||
|
||||
return LocalTime.ofSecondOfDay(randomTime);
|
||||
}
|
||||
|
||||
public static LocalTime time() {
|
||||
return between(LocalTime.MIN, LocalTime.MAX);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.localdate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.Month;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class LocalDateExampleUnitTest {
|
||||
private LocalDateExample date = new LocalDateExample();
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingOfMethod_thenLocalDate() {
|
||||
assertEquals("2020-01-08", date.getCustomDateOne(2020, 1, 8).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValuesWithMonthEnum_whenUsingOfMethod_thenLocalDate() {
|
||||
assertEquals("2020-01-08", date.getCustomDateTwo(2020, Month.JANUARY, 8).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingEpochDay_thenLocalDate() {
|
||||
assertEquals("2020-01-08", date.getDateFromEpochDay(18269).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingYearDay_thenLocalDate() {
|
||||
assertEquals("2020-01-08", date.getDateFromYearAndDayOfYear(2020, 8).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingParse_thenLocalDate() {
|
||||
assertEquals("2020-01-08", date.getDateFromString("2020-01-08").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValuesWithFormatter_whenUsingParse_thenLocalDate() {
|
||||
assertEquals("2020-01-08", date.getDateFromStringAndFormatter("8-Jan-2020", "d-MMM-yyyy").toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class LegacyRandomDateTimesUnitTest {
|
||||
|
||||
private static final Date MIN_DATE = new Date(Long.MIN_VALUE);
|
||||
private static final Date MAX_DATE = new Date(Long.MAX_VALUE);
|
||||
|
||||
@RepeatedTest(100)
|
||||
void givenARange_WhenGenTimestamp_ShouldBeInRange() {
|
||||
long aDay = TimeUnit.DAYS.toMillis(1);
|
||||
long now = new Date().getTime();
|
||||
|
||||
Date hundredYearsAgo = new Date(now - aDay * 365 * 100);
|
||||
Date tenDaysAgo = new Date(now - aDay * 10);
|
||||
|
||||
Date random = LegacyRandomDateTimes.between(hundredYearsAgo, tenDaysAgo);
|
||||
assertThat(random).isBetween(hundredYearsAgo, tenDaysAgo);
|
||||
}
|
||||
|
||||
@RepeatedTest(100)
|
||||
void givenNoRange_WhenGenTimestamp_ShouldGenerateRandomTimestamps() {
|
||||
Date random = LegacyRandomDateTimes.timestamp();
|
||||
|
||||
assertThat(random)
|
||||
.isNotNull()
|
||||
.isBetween(MIN_DATE, MAX_DATE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class RandomDateTimesUnitTest {
|
||||
|
||||
@RepeatedTest(100)
|
||||
void givenNoRange_WhenGenTimestamp_ShouldGenerateRandomTimestamps() {
|
||||
Instant random = RandomDateTimes.timestamp();
|
||||
|
||||
assertThat(random).isBetween(Instant.MIN, Instant.MAX);
|
||||
}
|
||||
|
||||
@RepeatedTest(100)
|
||||
void givenARange_WhenGenTimestamp_ShouldBeInRange() {
|
||||
Instant hundredYearsAgo = Instant.now().minus(Duration.ofDays(100 * 365));
|
||||
Instant tenDaysAgo = Instant.now().minus(Duration.ofDays(10));
|
||||
|
||||
Instant random = RandomDateTimes.between(hundredYearsAgo, tenDaysAgo);
|
||||
assertThat(random).isBetween(hundredYearsAgo, tenDaysAgo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class RandomDatesUnitTest {
|
||||
|
||||
@RepeatedTest(100)
|
||||
void givenNoRange_WhenGenDate_ShouldGenerateRandomDates() {
|
||||
LocalDate randomDay = RandomDates.date();
|
||||
|
||||
assertThat(randomDay).isAfter(LocalDate.MIN).isBefore(LocalDate.MAX);
|
||||
}
|
||||
|
||||
@RepeatedTest(100)
|
||||
void givenARange_WhenGenDate_ShouldBeInRange() {
|
||||
LocalDate start = LocalDate.of(1989, Month.OCTOBER, 14);
|
||||
LocalDate end = LocalDate.now();
|
||||
|
||||
LocalDate random = RandomDates.between(start, end);
|
||||
assertThat(random).isAfter(start).isBefore(end);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class RandomTimesUnitTest {
|
||||
|
||||
@RepeatedTest(100)
|
||||
void givenARange_WhenGenTime_ShouldBeInRange() {
|
||||
LocalTime morning = LocalTime.of(8, 30);
|
||||
LocalTime randomTime = RandomTimes.between(LocalTime.MIDNIGHT, morning);
|
||||
|
||||
assertThat(randomTime)
|
||||
.isAfter(LocalTime.MIDNIGHT).isBefore(morning)
|
||||
.isAfter(LocalTime.MIN).isBefore(LocalTime.MAX);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user