JAVA-617 Rename core-java-datetime-java8 to core-java-8-datetime
This commit is contained in:
16
core-java-modules/core-java-8-datetime/README.md
Normal file
16
core-java-modules/core-java-8-datetime/README.md
Normal file
@@ -0,0 +1,16 @@
|
||||
## Java 8+ Date and Time API
|
||||
|
||||
This module contains articles about the Date and Time API introduced with Java 8.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
|
||||
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)
|
||||
- [Get the Current Date, Time and Timestamp in Java 8](http://www.baeldung.com/current-date-time-and-timestamp-in-java-8)
|
||||
- [TemporalAdjuster in Java](http://www.baeldung.com/java-temporal-adjuster)
|
||||
- [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset)
|
||||
- [Differences Between ZonedDateTime and OffsetDateTime](https://www.baeldung.com/java-zoneddatetime-offsetdatetime)
|
||||
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
|
||||
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
|
||||
- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone)
|
||||
- [Comparing Dates in Java](https://www.baeldung.com/java-comparing-dates)
|
||||
- [[Next -->]](/core-java-modules/core-java-datetime-java8-2)
|
||||
74
core-java-modules/core-java-8-datetime/pom.xml
Normal file
74
core-java-modules/core-java-8-datetime/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,35 @@
|
||||
package com.baeldung.date.comparison;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static java.time.temporal.ChronoUnit.*;
|
||||
|
||||
public class DateTimeComparisonUtils {
|
||||
|
||||
public static boolean isSameDay(LocalDateTime timestamp, LocalDate localDateToCompare) {
|
||||
return timestamp.toLocalDate().isEqual(localDateToCompare);
|
||||
}
|
||||
|
||||
public static boolean isSameDay(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
|
||||
return timestamp.truncatedTo(DAYS).isEqual(timestampToCompare.truncatedTo(DAYS));
|
||||
}
|
||||
|
||||
public static boolean isSameHour(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
|
||||
return timestamp.truncatedTo(HOURS).isEqual(timestampToCompare.truncatedTo(HOURS));
|
||||
}
|
||||
|
||||
public static boolean isSameMinute(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
|
||||
return timestamp.truncatedTo(MINUTES).isEqual(timestampToCompare.truncatedTo(MINUTES));
|
||||
}
|
||||
|
||||
public static boolean isSameHour(ZonedDateTime zonedTimestamp, ZonedDateTime zonedTimestampToCompare) {
|
||||
return zonedTimestamp.truncatedTo(HOURS).isEqual(zonedTimestampToCompare.truncatedTo(HOURS));
|
||||
}
|
||||
|
||||
public static boolean isSameHour(ZonedDateTime zonedDateTime, LocalDateTime localDateTime, ZoneId zoneId) {
|
||||
return isSameHour(zonedDateTime, localDateTime.atZone(zoneId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.date.comparison;
|
||||
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class LegacyDateComparisonUtils {
|
||||
|
||||
public static boolean isSameDay(Date date, Date dateToCompare) {
|
||||
return DateUtils.isSameDay(date, dateToCompare);
|
||||
}
|
||||
|
||||
public static boolean isSameHour(Date date, Date dateToCompare) {
|
||||
return DateUtils.truncatedEquals(date, dateToCompare, Calendar.HOUR);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.util.Locale;
|
||||
|
||||
public class UseDateTimeFormatter {
|
||||
public String formatAsIsoDate(LocalDateTime localDateTime) {
|
||||
return localDateTime.format(DateTimeFormatter.ISO_DATE);
|
||||
}
|
||||
|
||||
public String formatCustom(LocalDateTime localDateTime, String pattern) {
|
||||
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
|
||||
}
|
||||
|
||||
public String formatWithStyleAndLocale(LocalDateTime localDateTime, FormatStyle formatStyle, Locale locale) {
|
||||
return localDateTime.format(DateTimeFormatter.ofLocalizedDateTime(formatStyle)
|
||||
.withLocale(locale));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class UseDuration {
|
||||
|
||||
public LocalTime modifyDates(LocalTime localTime, Duration duration) {
|
||||
return localTime.plus(duration);
|
||||
}
|
||||
|
||||
public Duration getDifferenceBetweenDates(LocalTime localTime1, LocalTime localTime2) {
|
||||
return Duration.between(localTime1, localTime2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
|
||||
class UseLocalDate {
|
||||
|
||||
LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth) {
|
||||
return LocalDate.of(year, month, dayOfMonth);
|
||||
}
|
||||
|
||||
LocalDate getLocalDateUsingParseMethod(String representation) {
|
||||
return LocalDate.parse(representation);
|
||||
}
|
||||
|
||||
LocalDate getLocalDateFromClock() {
|
||||
LocalDate localDate = LocalDate.now();
|
||||
return localDate;
|
||||
}
|
||||
|
||||
LocalDate getNextDay(LocalDate localDate) {
|
||||
return localDate.plusDays(1);
|
||||
}
|
||||
|
||||
LocalDate getPreviousDay(LocalDate localDate) {
|
||||
return localDate.minus(1, ChronoUnit.DAYS);
|
||||
}
|
||||
|
||||
DayOfWeek getDayOfWeek(LocalDate localDate) {
|
||||
DayOfWeek day = localDate.getDayOfWeek();
|
||||
return day;
|
||||
}
|
||||
|
||||
LocalDate getFirstDayOfMonth() {
|
||||
LocalDate firstDayOfMonth = LocalDate.now()
|
||||
.with(TemporalAdjusters.firstDayOfMonth());
|
||||
return firstDayOfMonth;
|
||||
}
|
||||
|
||||
boolean isLeapYear(LocalDate localDate) {
|
||||
return localDate.isLeapYear();
|
||||
}
|
||||
|
||||
LocalDateTime getStartOfDay(LocalDate localDate) {
|
||||
LocalDateTime startofDay = localDate.atStartOfDay();
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
LocalDateTime getStartOfDayOfLocalDate(LocalDate localDate) {
|
||||
LocalDateTime startofDay = LocalDateTime.of(localDate, LocalTime.MIDNIGHT);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
LocalDateTime getStartOfDayAtMinTime(LocalDate localDate) {
|
||||
LocalDateTime startofDay = localDate.atTime(LocalTime.MIN);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
LocalDateTime getStartOfDayAtMidnightTime(LocalDate localDate) {
|
||||
LocalDateTime startofDay = localDate.atTime(LocalTime.MIDNIGHT);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
LocalDateTime getEndOfDay(LocalDate localDate) {
|
||||
LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
|
||||
return endOfDay;
|
||||
}
|
||||
|
||||
LocalDateTime getEndOfDayFromLocalTime(LocalDate localDate) {
|
||||
LocalDateTime endOfDate = LocalTime.MAX.atDate(localDate);
|
||||
return endOfDate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoField;
|
||||
|
||||
public class UseLocalDateTime {
|
||||
|
||||
public LocalDateTime getLocalDateTimeUsingParseMethod(String representation) {
|
||||
return LocalDateTime.parse(representation);
|
||||
}
|
||||
|
||||
LocalDateTime getEndOfDayFromLocalDateTimeDirectly(LocalDateTime localDateTime) {
|
||||
LocalDateTime endOfDate = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());
|
||||
return endOfDate;
|
||||
}
|
||||
|
||||
LocalDateTime getEndOfDayFromLocalDateTime(LocalDateTime localDateTime) {
|
||||
LocalDateTime endOfDate = localDateTime.toLocalDate()
|
||||
.atTime(LocalTime.MAX);
|
||||
return endOfDate;
|
||||
}
|
||||
|
||||
LocalDateTime ofEpochSecond(int epochSecond, ZoneOffset zoneOffset) {
|
||||
return LocalDateTime.ofEpochSecond(epochSecond, 0, zoneOffset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class UseLocalTime {
|
||||
|
||||
LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds) {
|
||||
return LocalTime.of(hour, min, seconds);
|
||||
}
|
||||
|
||||
LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min) {
|
||||
return LocalTime.of(hour, min);
|
||||
}
|
||||
|
||||
LocalTime getLocalTimeUsingParseMethod(String timeRepresentation) {
|
||||
return LocalTime.parse(timeRepresentation);
|
||||
}
|
||||
|
||||
private LocalTime getLocalTimeFromClock() {
|
||||
return LocalTime.now();
|
||||
}
|
||||
|
||||
LocalTime addAnHour(LocalTime localTime) {
|
||||
return localTime.plus(1, ChronoUnit.HOURS);
|
||||
}
|
||||
|
||||
int getHourFromLocalTime(LocalTime localTime) {
|
||||
return localTime.getHour();
|
||||
}
|
||||
|
||||
LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute) {
|
||||
return localTime.withMinute(minute);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
public class UseOffsetDateTime {
|
||||
public OffsetDateTime offsetOfLocalDateTimeAndOffset(LocalDateTime localDateTime, ZoneOffset offset) {
|
||||
return OffsetDateTime.of(localDateTime, offset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
|
||||
class UsePeriod {
|
||||
|
||||
LocalDate modifyDates(LocalDate localDate, Period period) {
|
||||
return localDate.plus(period);
|
||||
}
|
||||
|
||||
Period getDifferenceBetweenDates(LocalDate localDate1, LocalDate localDate2) {
|
||||
return Period.between(localDate1, localDate2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class UseToInstant {
|
||||
|
||||
public LocalDateTime convertDateToLocalDate(Date date) {
|
||||
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
public LocalDateTime convertDateToLocalDate(Calendar calendar) {
|
||||
return LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoField;
|
||||
|
||||
class UseZonedDateTime {
|
||||
|
||||
ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) {
|
||||
return ZonedDateTime.of(localDateTime, zoneId);
|
||||
}
|
||||
|
||||
ZonedDateTime getZonedDateTimeUsingParseMethod(String parsableString) {
|
||||
return ZonedDateTime.parse(parsableString);
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDay(LocalDate localDate, ZoneId zone) {
|
||||
ZonedDateTime startOfDay = localDate.atStartOfDay()
|
||||
.atZone(zone);
|
||||
return startOfDay;
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDayShorthand(LocalDate localDate, ZoneId zone) {
|
||||
ZonedDateTime startOfDay = localDate.atStartOfDay(zone);
|
||||
return startOfDay;
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDayFromZonedDateTime(ZonedDateTime zonedDateTime) {
|
||||
ZonedDateTime startOfDay = zonedDateTime.toLocalDateTime()
|
||||
.toLocalDate()
|
||||
.atStartOfDay(zonedDateTime.getZone());
|
||||
return startOfDay;
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDayAtMinTime(ZonedDateTime zonedDateTime) {
|
||||
ZonedDateTime startOfDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0);
|
||||
return startOfDay;
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDayAtMidnightTime(ZonedDateTime zonedDateTime) {
|
||||
ZonedDateTime startOfDay = zonedDateTime.with(ChronoField.NANO_OF_DAY, 0);
|
||||
return startOfDay;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.temporaladjuster;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.Temporal;
|
||||
import java.time.temporal.TemporalAdjuster;
|
||||
|
||||
public class CustomTemporalAdjuster implements TemporalAdjuster {
|
||||
|
||||
@Override
|
||||
public Temporal adjustInto(Temporal temporal) {
|
||||
switch (DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK))) {
|
||||
case FRIDAY:
|
||||
return temporal.plus(3, ChronoUnit.DAYS);
|
||||
case SATURDAY:
|
||||
return temporal.plus(2, ChronoUnit.DAYS);
|
||||
default:
|
||||
return temporal.plus(1, ChronoUnit.DAYS);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.date.comparison;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
class DateTimeComparisonUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenLocalDateTimes_whenIsSameDay_thenCompareTrue() {
|
||||
LocalDateTime firstTimestamp = LocalDateTime.of(2019, 8, 10, 11, 00, 0);
|
||||
LocalDateTime secondTimestamp = firstTimestamp.plusHours(5);
|
||||
LocalDateTime thirdTimestamp = firstTimestamp.plusDays(1);
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameDay(firstTimestamp, secondTimestamp), is(true));
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameDay(secondTimestamp, thirdTimestamp), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLocalDateAndLocalDateTime_whenIsSameDay_thenCompareTrue() {
|
||||
LocalDate localDate = LocalDate.of(2019, 8, 10);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2019, 8, 10, 11, 30, 0);
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameDay(localDateTime, localDate), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLocalDateTimes_whenIsSameHour_thenCompareTrue() {
|
||||
LocalDateTime firstTimestamp = LocalDateTime.of(2019, 8, 10, 8, 00, 0);
|
||||
LocalDateTime secondTimestamp = firstTimestamp.plusMinutes(15);
|
||||
LocalDateTime thirdTimestamp = firstTimestamp.plusHours(2);
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameHour(firstTimestamp, secondTimestamp), is(true));
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameHour(secondTimestamp, thirdTimestamp), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLocalDateTimes_whenIsSameMinute_thenCompareTrue() {
|
||||
LocalDateTime firstTimestamp = LocalDateTime.of(2019, 8, 10, 8, 15, 0);
|
||||
LocalDateTime secondTimestamp = firstTimestamp.plusSeconds(30);
|
||||
LocalDateTime thirdTimestamp = firstTimestamp.plusMinutes(5);
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameMinute(firstTimestamp, secondTimestamp), is(true));
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameMinute(secondTimestamp, thirdTimestamp), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenZonedDateTimes_whenIsSameHour_thenCompareTrue() {
|
||||
ZonedDateTime zonedTimestamp = ZonedDateTime.of(2019, 8, 10, 8, 0, 0, 30,
|
||||
ZoneId.of("America/New_York"));
|
||||
ZonedDateTime zonedTimestampToCompare = ZonedDateTime.of(2019, 8, 10, 14, 0, 0, 0,
|
||||
ZoneId.of("Europe/Berlin"));
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameHour(zonedTimestamp, zonedTimestampToCompare), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenZonedDateTimeAndLocalDateTime_whenIsSameHour_thenCompareTrue() {
|
||||
ZonedDateTime zonedTimestamp = ZonedDateTime.of(2019, 8, 10, 8, 15, 0, 0,
|
||||
ZoneId.of("America/New_York"));
|
||||
LocalDateTime localTimestamp = LocalDateTime.of(2019, 8, 10, 14, 20, 0);
|
||||
ZoneId zoneId = ZoneId.of("Europe/Berlin");
|
||||
|
||||
assertThat(DateTimeComparisonUtils.isSameHour(zonedTimestamp, localTimestamp, zoneId), is(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.baeldung.date.comparison;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Java8DateTimeApiGeneralComparisonsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLocalDates_whenComparing_thenAssertsPass() {
|
||||
LocalDate firstDate = LocalDate.of(2019, 8, 10);
|
||||
LocalDate secondDate = LocalDate.of(2019, 7, 1);
|
||||
LocalDate thirdDate = LocalDate.of(2019, 7, 1); // same date as secondDate
|
||||
|
||||
assertThat(firstDate.isAfter(secondDate), is(true));
|
||||
assertThat(firstDate.isBefore(secondDate), is(false));
|
||||
|
||||
assertThat(firstDate.isEqual(secondDate), is(false));
|
||||
assertThat(firstDate.equals(secondDate), is(false));
|
||||
|
||||
assertThat(firstDate.compareTo(secondDate), is(1));
|
||||
assertThat(secondDate.compareTo(firstDate), is(-1));
|
||||
|
||||
assertThat(secondDate.isAfter(thirdDate), is(false));
|
||||
assertThat(secondDate.isBefore(thirdDate), is(false));
|
||||
assertThat(secondDate.isEqual(thirdDate), is(true));
|
||||
assertThat(secondDate.equals(thirdDate), is(true));
|
||||
assertThat(secondDate.compareTo(thirdDate), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimes_whenComparing_thenAssertsPass() {
|
||||
LocalDateTime firstTimestamp = LocalDateTime.of(2019, 8, 10, 11, 30, 0);
|
||||
LocalDateTime secondTimestamp = LocalDateTime.of(2019, 8, 10, 11, 15, 0);
|
||||
LocalDateTime thirdTimestamp = LocalDateTime.of(2019, 8, 10, 11, 15, 0); // same as secondTimestamp
|
||||
|
||||
assertThat(firstTimestamp.isAfter(secondTimestamp), is(true));
|
||||
assertThat(firstTimestamp.isBefore(secondTimestamp), is(false));
|
||||
|
||||
assertThat(firstTimestamp.isEqual(secondTimestamp), is(false));
|
||||
assertThat(firstTimestamp.equals(secondTimestamp), is(false));
|
||||
|
||||
assertThat(firstTimestamp.compareTo(secondTimestamp), is(1));
|
||||
assertThat(secondTimestamp.compareTo(firstTimestamp), is(-1));
|
||||
|
||||
assertThat(secondTimestamp.isAfter(thirdTimestamp), is(false));
|
||||
assertThat(secondTimestamp.isBefore(thirdTimestamp), is(false));
|
||||
assertThat(secondTimestamp.isEqual(thirdTimestamp), is(true));
|
||||
assertThat(secondTimestamp.compareTo(thirdTimestamp), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZonedDateTimes_whenComparing_thenAssertsPass() {
|
||||
ZonedDateTime timeInNewYork = ZonedDateTime.of(2019, 8, 10, 8, 0, 0, 0, ZoneId.of("America/New_York"));
|
||||
ZonedDateTime timeInBerlin = ZonedDateTime.of(2019, 8, 10, 14, 0, 0, 0, ZoneId.of("Europe/Berlin"));
|
||||
|
||||
assertThat(timeInNewYork.isAfter(timeInBerlin), is(false));
|
||||
assertThat(timeInNewYork.isBefore(timeInBerlin), is(false));
|
||||
|
||||
assertThat(timeInNewYork.isEqual(timeInBerlin), is(true));
|
||||
assertThat(timeInNewYork.equals(timeInBerlin), is(false));
|
||||
|
||||
assertThat(timeInNewYork.compareTo(timeInBerlin), is(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalTimes_whenComparing_thenAssertsPass() {
|
||||
LocalTime firstTime = LocalTime.of(8, 30);
|
||||
LocalTime secondTime = LocalTime.of(9, 45);
|
||||
|
||||
assertThat(firstTime.isAfter(secondTime), is(false));
|
||||
assertThat(firstTime.isBefore(secondTime), is(true));
|
||||
|
||||
assertThat(firstTime.equals(secondTime), is(false));
|
||||
|
||||
assertThat(firstTime.compareTo(secondTime), is(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMinMaxLocalTimes_whenComparing_thenAssertsPass() {
|
||||
LocalTime minTime = LocalTime.MIN;
|
||||
LocalTime time = LocalTime.of(8, 30);
|
||||
LocalTime maxTime = LocalTime.MAX;
|
||||
|
||||
assertThat(minTime.isBefore(time), is(true));
|
||||
assertThat(time.isBefore(maxTime), is(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.date.comparison;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
class LegacyDateComparisonUtilsUnitTest {
|
||||
|
||||
private Date toDate(LocalDateTime localDateTime) {
|
||||
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDatesWithDifferentHours_whenIsSameDay_thenReturnsTrue() {
|
||||
Date firstDate = toDate(LocalDateTime.of(2019, 8, 10, 11, 00, 00));
|
||||
Date secondDate = toDate(LocalDateTime.of(2019, 8, 10, 12, 00, 00));
|
||||
Date thirdDate = toDate(LocalDateTime.of(2019, 8, 15, 12, 00, 00));
|
||||
|
||||
assertThat(LegacyDateComparisonUtils.isSameDay(firstDate, secondDate), is(true));
|
||||
assertThat(LegacyDateComparisonUtils.isSameDay(secondDate, thirdDate), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDatesWithingSameHour_whenIsSameHour_thenReturnsTrue() {
|
||||
Date firstDate = toDate(LocalDateTime.of(2019, 8, 10, 11, 00, 00));
|
||||
Date secondDate = toDate(LocalDateTime.of(2019, 8, 10, 11, 15, 00));
|
||||
Date thirdDate = toDate(LocalDateTime.of(2019, 8, 10, 12, 00, 00));
|
||||
|
||||
assertThat(LegacyDateComparisonUtils.isSameHour(firstDate, secondDate), is(true));
|
||||
assertThat(LegacyDateComparisonUtils.isSameHour(secondDate, thirdDate), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDates__whenComparing_thenAssertsPass() {
|
||||
Date firstDate = toDate(LocalDateTime.of(2019, 8, 10, 0, 00, 00));
|
||||
Date secondDate = toDate(LocalDateTime.of(2019, 8, 15, 0, 00, 00));
|
||||
Date thirdDate = toDate(LocalDateTime.of(2019, 8, 15, 0, 00, 00)); // same date as secondDate
|
||||
|
||||
assertThat(firstDate.after(secondDate), is(false));
|
||||
assertThat(firstDate.before(secondDate), is(true));
|
||||
assertThat(firstDate.compareTo(secondDate), is(-1));
|
||||
assertThat(firstDate.equals(secondDate), is(false));
|
||||
|
||||
assertThat(thirdDate.after(secondDate), is(false));
|
||||
assertThat(thirdDate.before(secondDate), is(false));
|
||||
assertThat(thirdDate.compareTo(secondDate), is(0));
|
||||
assertThat(thirdDate.equals(secondDate), is(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.dateapi;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class ConversionExample {
|
||||
public static void main(String[] args) {
|
||||
Instant instantFromCalendar = GregorianCalendar.getInstance().toInstant();
|
||||
ZonedDateTime zonedDateTimeFromCalendar = new GregorianCalendar().toZonedDateTime();
|
||||
Date dateFromInstant = Date.from(Instant.now());
|
||||
GregorianCalendar calendarFromZonedDateTime = GregorianCalendar.from(ZonedDateTime.now());
|
||||
Instant instantFromDate = new Date().toInstant();
|
||||
ZoneId zoneIdFromTimeZone = TimeZone.getTimeZone("PST").toZoneId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.dateapi;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaDurationUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenATimePlus30Seconds_whenRequestingDuration_thenExpect30() {
|
||||
LocalTime initialTime = LocalTime.of(6, 30, 0);
|
||||
LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30));
|
||||
|
||||
long seconds = Duration.between(initialTime, finalTime)
|
||||
.getSeconds();
|
||||
|
||||
assertThat(seconds).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenATimePlus30Seconds_whenRequestingSecondsBetween_thenExpect30() {
|
||||
LocalTime initialTime = LocalTime.of(6, 30, 0);
|
||||
LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30));
|
||||
|
||||
long seconds = ChronoUnit.SECONDS.between(initialTime, finalTime);
|
||||
|
||||
assertThat(seconds).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
Instant start = Instant.parse("2017-10-03T10:15:30.00Z");
|
||||
Instant end = Instant.parse("2017-10-03T10:16:30.00Z");
|
||||
|
||||
Duration duration = Duration.between(start, end);
|
||||
|
||||
assertFalse(duration.isNegative());
|
||||
|
||||
assertEquals(60, duration.getSeconds());
|
||||
assertEquals(1, duration.toMinutes());
|
||||
|
||||
Duration fromDays = Duration.ofDays(1);
|
||||
assertEquals(86400, fromDays.getSeconds());
|
||||
|
||||
Duration fromMinutes = Duration.ofMinutes(60);
|
||||
assertEquals(1, fromMinutes.toHours());
|
||||
|
||||
assertEquals(120, duration.plusSeconds(60)
|
||||
.getSeconds());
|
||||
assertEquals(30, duration.minusSeconds(30)
|
||||
.getSeconds());
|
||||
|
||||
assertEquals(120, duration.plus(60, ChronoUnit.SECONDS)
|
||||
.getSeconds());
|
||||
assertEquals(30, duration.minus(30, ChronoUnit.SECONDS)
|
||||
.getSeconds());
|
||||
|
||||
Duration fromChar1 = Duration.parse("P1DT1H10M10.5S");
|
||||
Duration fromChar2 = Duration.parse("PT10M");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.dateapi;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaPeriodUnitTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(JavaPeriodUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenADatePlus5Days_whenRequestingPeriod_thenExpectFive() {
|
||||
LocalDate initialDate = LocalDate.parse("2007-05-10");
|
||||
LocalDate finalDate = initialDate.plus(Period.ofDays(5));
|
||||
|
||||
int days = Period.between(initialDate, finalDate)
|
||||
.getDays();
|
||||
|
||||
assertThat(days).isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenADatePlus5Days_whenRequestingDaysBetween_thenExpectFive() {
|
||||
LocalDate initialDate = LocalDate.parse("2007-05-10");
|
||||
LocalDate finalDate = initialDate.plus(Period.ofDays(5));
|
||||
|
||||
long days = ChronoUnit.DAYS.between(initialDate, finalDate);
|
||||
|
||||
assertThat(days).isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTestPeriod_thenOk() {
|
||||
|
||||
LocalDate startDate = LocalDate.of(2015, 2, 15);
|
||||
LocalDate endDate = LocalDate.of(2017, 1, 21);
|
||||
|
||||
Period period = Period.between(startDate, endDate);
|
||||
|
||||
LOG.info(String.format("Years:%d months:%d days:%d", period.getYears(), period.getMonths(), period.getDays()));
|
||||
|
||||
assertFalse(period.isNegative());
|
||||
assertEquals(56, period.plusDays(50)
|
||||
.getDays());
|
||||
assertEquals(9, period.minusMonths(2)
|
||||
.getMonths());
|
||||
|
||||
Period fromUnits = Period.of(3, 10, 10);
|
||||
Period fromDays = Period.ofDays(50);
|
||||
Period fromMonths = Period.ofMonths(5);
|
||||
Period fromYears = Period.ofYears(10);
|
||||
Period fromWeeks = Period.ofWeeks(40);
|
||||
|
||||
assertEquals(280, fromWeeks.getDays());
|
||||
|
||||
Period fromCharYears = Period.parse("P2Y");
|
||||
assertEquals(2, fromCharYears.getYears());
|
||||
Period fromCharUnits = Period.parse("P2Y3M5D");
|
||||
assertEquals(5, fromCharUnits.getDays());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.baeldung.dateapi;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Month;
|
||||
import java.time.YearMonth;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JavaUtilTimeUnitTest {
|
||||
|
||||
@Test
|
||||
public void currentTime() {
|
||||
final LocalDate now = LocalDate.now();
|
||||
|
||||
System.out.println(now);
|
||||
// there is not much to test here
|
||||
}
|
||||
|
||||
@Test
|
||||
public void specificTime() {
|
||||
LocalDate birthDay = LocalDate.of(1990, Month.DECEMBER, 15);
|
||||
|
||||
System.out.println(birthDay);
|
||||
// there is not much to test here
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extractMonth() {
|
||||
Month month = LocalDate.of(1990, Month.DECEMBER, 15).getMonth();
|
||||
|
||||
assertThat(month).isEqualTo(Month.DECEMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subtractTime() {
|
||||
LocalDateTime fiveHoursBefore = LocalDateTime.of(1990, Month.DECEMBER, 15, 15, 0).minusHours(5);
|
||||
|
||||
assertThat(fiveHoursBefore.getHour()).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alterField() {
|
||||
LocalDateTime inJune = LocalDateTime.of(1990, Month.DECEMBER, 15, 15, 0).with(Month.JUNE);
|
||||
|
||||
assertThat(inJune.getMonth()).isEqualTo(Month.JUNE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void truncate() {
|
||||
LocalTime truncated = LocalTime.of(15, 12, 34).truncatedTo(ChronoUnit.HOURS);
|
||||
|
||||
assertThat(truncated).isEqualTo(LocalTime.of(15, 0, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTimeSpan() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime hourLater = now.plusHours(1);
|
||||
Duration span = Duration.between(now, hourLater);
|
||||
|
||||
assertThat(span).isEqualTo(Duration.ofHours(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAndParse() throws ParseException {
|
||||
LocalDate someDate = LocalDate.of(2016, 12, 7);
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
String formattedDate = someDate.format(formatter);
|
||||
LocalDate parsedDate = LocalDate.parse(formattedDate, formatter);
|
||||
|
||||
assertThat(formattedDate).isEqualTo("2016-12-07");
|
||||
assertThat(parsedDate).isEqualTo(someDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void daysInMonth() {
|
||||
int daysInMonth = YearMonth.of(1990, 2).lengthOfMonth();
|
||||
|
||||
assertThat(daysInMonth).isEqualTo(28);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Month;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseDateTimeFormatterUnitTest {
|
||||
private final UseDateTimeFormatter subject = new UseDateTimeFormatter();
|
||||
private final LocalDateTime localDateTime = LocalDateTime.of(2015, Month.JANUARY, 25, 6, 30);
|
||||
|
||||
@Test
|
||||
public void givenALocalDate_whenFormattingAsIso_thenPass() {
|
||||
String result = subject.formatAsIsoDate(localDateTime);
|
||||
|
||||
assertThat(result).isEqualTo("2015-01-25");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenALocalDate_whenFormattingWithPattern_thenPass() {
|
||||
String result = subject.formatCustom(localDateTime, "yyyy/MM/dd");
|
||||
|
||||
assertThat(result).isEqualTo("2015/01/25");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenALocalDate_whenFormattingWithStyleAndLocale_thenPass() {
|
||||
String result = subject.formatWithStyleAndLocale(localDateTime, FormatStyle.MEDIUM, Locale.UK);
|
||||
|
||||
assertThat(result).isEqualTo("25 Jan 2015, 06:30:00");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Month;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseLocalDateTimeUnitTest {
|
||||
|
||||
private UseLocalDateTime useLocalDateTime = new UseLocalDateTime();
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingParse_thenLocalDateTime() {
|
||||
assertEquals(LocalDate.of(2016, Month.MAY, 10), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30")
|
||||
.toLocalDate());
|
||||
assertEquals(LocalTime.of(6, 30), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30")
|
||||
.toLocalTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTime_whenSettingEndOfDay_thenReturnLastMomentOfDay() {
|
||||
LocalDateTime givenTimed = LocalDateTime.parse("2018-06-23T05:55:55");
|
||||
|
||||
LocalDateTime endOfDayFromGivenDirectly = useLocalDateTime.getEndOfDayFromLocalDateTimeDirectly(givenTimed);
|
||||
LocalDateTime endOfDayFromGiven = useLocalDateTime.getEndOfDayFromLocalDateTime(givenTimed);
|
||||
|
||||
assertThat(endOfDayFromGivenDirectly).isEqualTo(endOfDayFromGiven);
|
||||
assertThat(endOfDayFromGivenDirectly.toLocalTime()).isEqualTo(LocalTime.MAX);
|
||||
assertThat(endOfDayFromGivenDirectly.toString()).isEqualTo("2018-06-23T23:59:59.999999999");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeInFebruary_whenRequestingMonth_thenMonthIsFebruary() {
|
||||
LocalDateTime givenLocalDateTime = LocalDateTime.of(2015, Month.FEBRUARY, 20, 6, 30);
|
||||
|
||||
assertThat(givenLocalDateTime.getMonth()).isEqualTo(Month.FEBRUARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTime_whenManipulating_thenResultIsAsExpected() {
|
||||
LocalDateTime givenLocalDateTime = LocalDateTime.parse("2015-02-20T06:30:00");
|
||||
|
||||
LocalDateTime manipulatedLocalDateTime = givenLocalDateTime.plusDays(1);
|
||||
manipulatedLocalDateTime = manipulatedLocalDateTime.minusHours(2);
|
||||
|
||||
assertThat(manipulatedLocalDateTime).isEqualTo(LocalDateTime.of(2015, Month.FEBRUARY, 21, 4, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestTimeFromEpoch_thenResultIsAsExpected() {
|
||||
LocalDateTime result = useLocalDateTime.ofEpochSecond(1465817690, ZoneOffset.UTC);
|
||||
|
||||
assertThat(result.toString()).isEqualTo("2016-06-13T11:34:50");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseLocalDateUnitTest {
|
||||
|
||||
private UseLocalDate useLocalDate = new UseLocalDate();
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingFactoryOf_thenLocalDate() {
|
||||
assertEquals("2016-05-10", useLocalDate.getLocalDateUsingFactoryOfMethod(2016, 5, 10)
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingParse_thenLocalDate() {
|
||||
assertEquals("2016-05-10", useLocalDate.getLocalDateUsingParseMethod("2016-05-10")
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingClock_thenLocalDate() {
|
||||
assertEquals(LocalDate.now(), useLocalDate.getLocalDateFromClock());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_whenUsingPlus_thenNextDay() {
|
||||
assertEquals(LocalDate.now()
|
||||
.plusDays(1), useLocalDate.getNextDay(LocalDate.now()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_whenUsingMinus_thenPreviousDay() {
|
||||
assertEquals(LocalDate.now()
|
||||
.minusDays(1), useLocalDate.getPreviousDay(LocalDate.now()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToday_whenUsingGetDayOfWeek_thenDayOfWeek() {
|
||||
assertEquals(DayOfWeek.SUNDAY, useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth() {
|
||||
assertEquals(1, useLocalDate.getFirstDayOfMonth()
|
||||
.getDayOfMonth());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight() {
|
||||
assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"), useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDate_whenSettingStartOfDay_thenReturnMidnightInAllCases() {
|
||||
LocalDate given = LocalDate.parse("2018-06-23");
|
||||
|
||||
LocalDateTime startOfDayWithMethod = useLocalDate.getStartOfDay(given);
|
||||
LocalDateTime startOfDayOfLocalDate = useLocalDate.getStartOfDayOfLocalDate(given);
|
||||
LocalDateTime startOfDayWithMin = useLocalDate.getStartOfDayAtMinTime(given);
|
||||
LocalDateTime startOfDayWithMidnight = useLocalDate.getStartOfDayAtMidnightTime(given);
|
||||
|
||||
assertThat(startOfDayWithMethod).isEqualTo(startOfDayWithMin)
|
||||
.isEqualTo(startOfDayWithMidnight)
|
||||
.isEqualTo(startOfDayOfLocalDate)
|
||||
.isEqualTo(LocalDateTime.parse("2018-06-23T00:00:00"));
|
||||
assertThat(startOfDayWithMin.toLocalTime()).isEqualTo(LocalTime.MIDNIGHT);
|
||||
assertThat(startOfDayWithMin.toString()).isEqualTo("2018-06-23T00:00");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDate_whenSettingEndOfDay_thenReturnLastMomentOfDay() {
|
||||
LocalDate given = LocalDate.parse("2018-06-23");
|
||||
|
||||
LocalDateTime endOfDayWithMax = useLocalDate.getEndOfDay(given);
|
||||
LocalDateTime endOfDayFromLocalTime = useLocalDate.getEndOfDayFromLocalTime(given);
|
||||
|
||||
assertThat(endOfDayWithMax).isEqualTo(endOfDayFromLocalTime);
|
||||
assertThat(endOfDayWithMax.toLocalTime()).isEqualTo(LocalTime.MAX);
|
||||
assertThat(endOfDayWithMax.toString()).isEqualTo("2018-06-23T23:59:59.999999999");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheYear2000_whenCheckingForLeapYear_thenReturnTrue() {
|
||||
LocalDate given = LocalDate.parse("2000-06-23");
|
||||
|
||||
boolean leapYear = useLocalDate.isLeapYear(given);
|
||||
|
||||
assertThat(leapYear).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheYear2004_whenCheckingForLeapYear_thenReturnTrue() {
|
||||
LocalDate given = LocalDate.parse("2004-06-23");
|
||||
|
||||
boolean leapYear = useLocalDate.isLeapYear(given);
|
||||
|
||||
assertThat(leapYear).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheYear2019_whenCheckingForLeapYear_thenReturnFalse() {
|
||||
LocalDate given = LocalDate.parse("2019-06-23");
|
||||
|
||||
boolean leapYear = useLocalDate.isLeapYear(given);
|
||||
|
||||
assertThat(leapYear).isEqualTo(false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseLocalTimeUnitTest {
|
||||
|
||||
private UseLocalTime useLocalTime = new UseLocalTime();
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingFactoryOf_thenLocalTime() {
|
||||
Assert.assertEquals("07:07:07", useLocalTime.getLocalTimeUsingFactoryOfMethod(7, 7, 7)
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingFactoryOfWithoutSeconds_thenLocalTime() {
|
||||
Assert.assertEquals("07:07", useLocalTime.getLocalTimeUsingFactoryOfMethod(7, 7)
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingParse_thenLocalTime() {
|
||||
Assert.assertEquals("06:30", useLocalTime.getLocalTimeUsingParseMethod("06:30")
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTime_whenAddHour_thenLocalTime() {
|
||||
Assert.assertEquals("07:30", useLocalTime.addAnHour(LocalTime.of(6, 30))
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHourFromLocalTime() {
|
||||
Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1, 1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLocalTimeWithMinuteSetToValue() {
|
||||
Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10, 10), 20));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Month;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseOffsetDateTimeUnitTest {
|
||||
private final UseOffsetDateTime subject = new UseOffsetDateTime();
|
||||
|
||||
@Test
|
||||
public void givenAZoneOffSetAndLocalDateTime_whenCombing_thenValidResult() {
|
||||
ZoneOffset offset = ZoneOffset.of("+02:00");
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2015, Month.FEBRUARY, 20, 6, 30);
|
||||
|
||||
OffsetDateTime result = subject.offsetOfLocalDateTimeAndOffset(localDateTime, offset);
|
||||
|
||||
assertThat(result.toString()).isEqualTo("2015-02-20T06:30+02:00");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UsePeriodUnitTest {
|
||||
UsePeriod usingPeriod = new UsePeriod();
|
||||
|
||||
@Test
|
||||
public void givenPeriodAndLocalDate_thenCalculateModifiedDate() {
|
||||
Period period = Period.ofDays(1);
|
||||
LocalDate localDate = LocalDate.parse("2007-05-10");
|
||||
Assert.assertEquals(localDate.plusDays(1), usingPeriod.modifyDates(localDate, period));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDates_thenGetPeriod() {
|
||||
LocalDate localDate1 = LocalDate.parse("2007-05-10");
|
||||
LocalDate localDate2 = LocalDate.parse("2007-05-15");
|
||||
|
||||
Assert.assertEquals(Period.ofDays(5), usingPeriod.getDifferenceBetweenDates(localDate1, localDate2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseTimeZoneUnitTest {
|
||||
|
||||
/* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */
|
||||
|
||||
String timeZone = "Asia/Singapore";
|
||||
|
||||
private static final String PATTERN = "E yyyy-MM-dd HH:mm:ss a";
|
||||
|
||||
@Test
|
||||
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava7_ThenTimeZoneIsSetSuccessfully() {
|
||||
Date nowUtc = new Date();
|
||||
TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone);
|
||||
|
||||
Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore);
|
||||
nowAsiaSingapore.setTime(nowUtc);
|
||||
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
|
||||
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
|
||||
|
||||
System.out.println(String.format("Java7: Time now in '%s' is '%s'", nowAsiaSingapore.getTimeZone()
|
||||
.getID(), simpleDateFormat.format(nowAsiaSingapore.getTime())));
|
||||
|
||||
Assert.assertEquals(nowUtc.toInstant().getEpochSecond(), nowAsiaSingapore.toInstant().getEpochSecond());
|
||||
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getTimeZone());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava8_ThenTimeZoneIsSetSuccessfully() {
|
||||
Instant nowUtc = Instant.now();
|
||||
ZoneId asiaSingapore = ZoneId.of(timeZone);
|
||||
|
||||
ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);
|
||||
|
||||
System.out.println(String.format("Java8: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
|
||||
nowAsiaSingapore.format(DateTimeFormatter.ofPattern(PATTERN))));
|
||||
|
||||
Assert.assertEquals(nowUtc.getEpochSecond(), nowAsiaSingapore.toEpochSecond());
|
||||
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJodaTime_ThenTimeZoneIsSetSuccessfully() {
|
||||
org.joda.time.Instant nowUtc = org.joda.time.Instant.now();
|
||||
DateTimeZone asiaSingapore = DateTimeZone.forID(timeZone);
|
||||
|
||||
DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore);
|
||||
|
||||
System.out.println(String.format("Joda-time: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
|
||||
nowAsiaSingapore.toString(PATTERN)));
|
||||
|
||||
Assert.assertEquals(nowUtc.toInstant().getMillis(), nowAsiaSingapore.toInstant().getMillis());
|
||||
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseToInstantUnitTest {
|
||||
|
||||
private UseToInstant subject = new UseToInstant();
|
||||
|
||||
@Test
|
||||
public void givenAGregorianCalenderDate_whenConvertingToLocalDate_thenAsExpected() {
|
||||
GregorianCalendar givenCalender = new GregorianCalendar(2018, Calendar.JULY, 28);
|
||||
|
||||
LocalDateTime localDateTime = subject.convertDateToLocalDate(givenCalender);
|
||||
|
||||
assertThat(localDateTime).isEqualTo("2018-07-28T00:00:00");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenADate_whenConvertingToLocalDate_thenAsExpected() {
|
||||
Date givenDate = new Date(1465817690000L);
|
||||
|
||||
LocalDateTime localDateTime = subject.convertDateToLocalDate(givenDate);
|
||||
|
||||
assertThat(localDateTime).isEqualTo("2016-06-13T13:34:50");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseZonedDateTimeUnitTest {
|
||||
|
||||
private UseZonedDateTime zonedDateTime = new UseZonedDateTime();
|
||||
|
||||
@Test
|
||||
public void givenZoneId_thenZonedDateTime() {
|
||||
ZoneId zoneId = ZoneId.of("Europe/Paris");
|
||||
ZonedDateTime zonedDatetime = zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
|
||||
Assert.assertEquals(zoneId, ZoneId.from(zonedDatetime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingZones_thenAtLeastOneIsReturned() {
|
||||
Set<String> allZoneIds = ZoneId.getAvailableZoneIds();
|
||||
|
||||
assertThat(allZoneIds.size()).isGreaterThan(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateOrZoned_whenSettingStartOfDay_thenReturnMidnightInAllCases() {
|
||||
LocalDate given = LocalDate.parse("2018-06-23");
|
||||
ZoneId zone = ZoneId.of("Europe/Paris");
|
||||
ZonedDateTime zonedGiven = ZonedDateTime.of(given, LocalTime.NOON, zone);
|
||||
|
||||
ZonedDateTime startOfOfDayWithMethod = zonedDateTime.getStartOfDay(given, zone);
|
||||
ZonedDateTime startOfOfDayWithShorthandMethod = zonedDateTime.getStartOfDayShorthand(given, zone);
|
||||
ZonedDateTime startOfOfDayFromZonedDateTime = zonedDateTime.getStartOfDayFromZonedDateTime(zonedGiven);
|
||||
ZonedDateTime startOfOfDayAtMinTime = zonedDateTime.getStartOfDayAtMinTime(zonedGiven);
|
||||
ZonedDateTime startOfOfDayAtMidnightTime = zonedDateTime.getStartOfDayAtMidnightTime(zonedGiven);
|
||||
|
||||
assertThat(startOfOfDayWithMethod).isEqualTo(startOfOfDayWithShorthandMethod)
|
||||
.isEqualTo(startOfOfDayFromZonedDateTime)
|
||||
.isEqualTo(startOfOfDayAtMinTime)
|
||||
.isEqualTo(startOfOfDayAtMidnightTime);
|
||||
assertThat(startOfOfDayWithMethod.toLocalTime()).isEqualTo(LocalTime.MIDNIGHT);
|
||||
assertThat(startOfOfDayWithMethod.toLocalTime()
|
||||
.toString()).isEqualTo("00:00");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAStringWithTimeZone_whenParsing_thenEqualsExpected() {
|
||||
ZonedDateTime resultFromString = zonedDateTime.getZonedDateTimeUsingParseMethod("2015-05-03T10:15:30+01:00[Europe/Paris]");
|
||||
ZonedDateTime resultFromLocalDateTime = ZonedDateTime.of(2015, 5, 3, 11, 15, 30, 0, ZoneId.of("Europe/Paris"));
|
||||
|
||||
assertThat(resultFromString.getZone()).isEqualTo(ZoneId.of("Europe/Paris"));
|
||||
assertThat(resultFromLocalDateTime.getZone()).isEqualTo(ZoneId.of("Europe/Paris"));
|
||||
|
||||
assertThat(resultFromString).isEqualTo(resultFromLocalDateTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.temporaladjusters;
|
||||
|
||||
import com.baeldung.temporaladjuster.CustomTemporalAdjuster;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.temporal.TemporalAdjuster;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CustomTemporalAdjusterUnitTest {
|
||||
|
||||
private static final TemporalAdjuster NEXT_WORKING_DAY = new CustomTemporalAdjuster();
|
||||
|
||||
@Test
|
||||
public void whenAdjustAndImplementInterface_thenNextWorkingDay() {
|
||||
LocalDate localDate = LocalDate.of(2017, 07, 8);
|
||||
CustomTemporalAdjuster temporalAdjuster = new CustomTemporalAdjuster();
|
||||
LocalDate nextWorkingDay = localDate.with(temporalAdjuster);
|
||||
|
||||
assertEquals("2017-07-10", nextWorkingDay.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAdjust_thenNextWorkingDay() {
|
||||
LocalDate localDate = LocalDate.of(2017, 07, 8);
|
||||
LocalDate date = localDate.with(NEXT_WORKING_DAY);
|
||||
|
||||
assertEquals("2017-07-10", date.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAdjust_thenFourteenDaysAfterDate() {
|
||||
LocalDate localDate = LocalDate.of(2017, 07, 8);
|
||||
TemporalAdjuster temporalAdjuster = (t) -> t.plus(Period.ofDays(14));
|
||||
LocalDate result = localDate.with(temporalAdjuster);
|
||||
|
||||
String fourteenDaysAfterDate = "2017-07-22";
|
||||
|
||||
assertEquals(fourteenDaysAfterDate, result.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.temporaladjusters;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TemporalAdjustersUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAdjust_thenNextSunday() {
|
||||
LocalDate localDate = LocalDate.of(2017, 07, 8);
|
||||
LocalDate nextSunday = localDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
|
||||
|
||||
String expected = "2017-07-09";
|
||||
|
||||
Assert.assertEquals(expected, nextSunday.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user