added a test for DateUtils

This commit is contained in:
Fabio Formosa
2022-10-27 22:58:56 +02:00
parent 4013c4c08f
commit 0c33eda68c
3 changed files with 40 additions and 6 deletions

View File

@@ -6,17 +6,26 @@
<version>3.1.1-SNAPSHOT</version>
</parent>
<artifactId>quartz-manager-common</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -3,16 +3,21 @@ package it.fabioformosa.quartzmanager.api.common.utils;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class DateUtils {
static public Date fromLocaleDateTimeToDate(LocalDateTime localDateTime){
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
static public Date fromLocalDateTimeToDate(LocalDateTime localDateTime){
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant().truncatedTo(ChronoUnit.MILLIS));
}
static public Date getHoursFromNow(long hours){
return DateUtils.fromLocaleDateTimeToDate(LocalDateTime.now().plus(Duration.ofHours(hours)));
static public LocalDateTime fromDateToLocalDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().truncatedTo(ChronoUnit.MILLIS);
}
static public Date addHoursToNow(long hours){
return DateUtils.fromLocalDateTimeToDate(LocalDateTime.now().plus(Duration.ofHours(hours)));
}
}

View File

@@ -0,0 +1,20 @@
package it.fabioformosa.quartzmanager.api.common.utils;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
class DateUtilsTest {
@Test
public void givenALocaleDatetime_whenTheConversionIsCalled_shouldGetADate(){
LocalDateTime originalLocalDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
Date date = DateUtils.fromLocalDateTimeToDate(originalLocalDateTime);
LocalDateTime convertedLocalDateTime = DateUtils.fromDateToLocalDateTime(date);
Assertions.assertThat(convertedLocalDateTime).isEqualTo(originalLocalDateTime);
}
}