BAEL-1901 Add hours to Date in Java (#4599)

* BAEL-1901 Add hours to Date in Java

* BAEL-1901 : Editor Review Changes

* BAEL-1901 using assertThat for assertions
This commit is contained in:
Shubhra Srivastava 2018-07-08 20:57:42 +05:30 committed by KevinGilmore
parent e5f2714ff8
commit 387d93e905
2 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,54 @@
package com.baeldung.datetime;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;
public class AddHoursToDate {
public Date addHoursToJavaUtilDate(Date date, int hours) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hours);
return calendar.getTime();
}
public Date addHoursToDateUsingInstant(Date date, int hours) {
return Date.from(date.toInstant()
.plus(Duration.ofHours(hours)));
}
public LocalDateTime addHoursToLocalDateTime(LocalDateTime localDateTime, int hours) {
return localDateTime.plusHours(hours);
}
public LocalDateTime subtractHoursToLocalDateTime(LocalDateTime localDateTime, int hours) {
return localDateTime.minusHours(hours);
}
public ZonedDateTime addHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
return zonedDateTime.plusHours(hours);
}
public ZonedDateTime subtractHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
return zonedDateTime.minusHours(hours);
}
public Instant addHoursToInstant(Instant instant, int hours) {
return instant.plus(hours, ChronoUnit.HOURS);
}
public Instant subtractHoursToInstant(Instant instant, int hours) {
return instant.minus(hours, ChronoUnit.HOURS);
}
public Date addHoursWithApacheCommons(Date date, int hours) {
return DateUtils.addHours(date, hours);
}
}

View File

@ -0,0 +1,116 @@
package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.junit.Test;
public class AddHoursToDateUnitTest {
private final AddHoursToDate addHoursToDateObj = new AddHoursToDate();
@Test
public void givenJavaUtilDate_whenPositiveHours_thenAddHours() {
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 7, 0).getTime();
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursToJavaUtilDate(actualDate, 2));
}
@Test
public void givenJavaUtilDate_whenNegativeHours_thenMinusHours() {
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime();
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursToJavaUtilDate(actualDate, -2));
}
@Test
public void givenJavaUtilDate_whenUsingToInstantAndPostiveHours_thenAddHours() {
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 7, 0).getTime();
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursToDateUsingInstant(actualDate, 2));
}
@Test
public void givenJavaUtilDate_whenUsingToInstantAndNegativeHours_thenAddHours() {
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime();
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursToDateUsingInstant(actualDate, -2));
}
@Test
public void givenLocalDateTime_whenUsingAddHoursToLocalDateTime_thenAddHours() {
LocalDateTime actualDateTime = LocalDateTime.of(2018, Month.JUNE, 25, 5, 0);
LocalDateTime expectedDateTime = LocalDateTime.of(2018, Month.JUNE, 25, 7, 0);
assertThat(expectedDateTime).isEqualTo(addHoursToDateObj.addHoursToLocalDateTime(actualDateTime, 2));
}
@Test
public void givenLocalDateTime_whenUsingMinusHoursToLocalDateTime_thenMinusHours() {
LocalDateTime actualDateTime = LocalDateTime.of(2018, Month.JUNE, 25, 5, 0);
LocalDateTime expectedDateTime = LocalDateTime.of(2018, Month.JUNE, 25, 3, 0);
assertThat(expectedDateTime).isEqualTo(addHoursToDateObj.subtractHoursToLocalDateTime(actualDateTime, 2));
}
@Test
public void givenZonedDateTime_whenUsingAddHoursToZonedDateTime_thenAddHours() {
ZonedDateTime actualZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2018, Month.JUNE, 25, 5, 0), ZoneId.systemDefault());
ZonedDateTime expectedZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2018, Month.JUNE, 25, 7, 0), ZoneId.systemDefault());
assertThat(expectedZonedDateTime).isEqualTo(addHoursToDateObj.addHoursToZonedDateTime(actualZonedDateTime, 2));
}
@Test
public void givenZonedDateTime_whenUsingMinusHoursToZonedDateTime_thenMinusHours() {
ZonedDateTime actualZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2018, Month.JUNE, 25, 5, 0), ZoneId.systemDefault());
ZonedDateTime expectedZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2018, Month.JUNE, 25, 3, 0), ZoneId.systemDefault());
assertThat(expectedZonedDateTime).isEqualTo(addHoursToDateObj.subtractHoursToZonedDateTime(actualZonedDateTime, 2));
}
@Test
public void givenJavaUtilDate_whenUsingPositiveHrsAndAddHoursWithApacheCommons_thenAddHours() {
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 7, 0).getTime();
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursWithApacheCommons(actualDate, 2));
}
@Test
public void givenJavaUtilDate_whenUsingNegativeHrsAndAddHoursWithApacheCommons_thenMinusHours() {
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 7, 0).getTime();
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursWithApacheCommons(actualDate, -2));
}
@Test
public void givenInstant_whenUsingAddHoursToInstant_thenAddHours() {
Instant actualValue = Instant.parse("2018-06-25T05:12:35Z");
Instant expectedValue = Instant.parse("2018-06-25T07:12:35Z");
assertThat(expectedValue).isEqualTo(addHoursToDateObj.addHoursToInstant(actualValue, 2));
}
@Test
public void givenInstant_whenUsingSubtractHoursToInstant_thenMinusHours() {
Instant actualValue = Instant.parse("2018-06-25T07:12:35Z");
Instant expectedValue = Instant.parse("2018-06-25T05:12:35Z");
assertThat(expectedValue).isEqualTo(addHoursToDateObj.subtractHoursToInstant(actualValue, 2));
}
}