From 387d93e9054e7ba7212520217e9db3bf2af38664 Mon Sep 17 00:00:00 2001 From: Shubhra Srivastava Date: Sun, 8 Jul 2018 20:57:42 +0530 Subject: [PATCH] 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 --- .../com/baeldung/datetime/AddHoursToDate.java | 54 ++++++++ .../datetime/AddHoursToDateUnitTest.java | 116 ++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/datetime/AddHoursToDate.java create mode 100644 core-java-8/src/test/java/com/baeldung/datetime/AddHoursToDateUnitTest.java diff --git a/core-java-8/src/main/java/com/baeldung/datetime/AddHoursToDate.java b/core-java-8/src/main/java/com/baeldung/datetime/AddHoursToDate.java new file mode 100644 index 0000000000..d9636656b5 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/datetime/AddHoursToDate.java @@ -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); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/datetime/AddHoursToDateUnitTest.java b/core-java-8/src/test/java/com/baeldung/datetime/AddHoursToDateUnitTest.java new file mode 100644 index 0000000000..9e20a94843 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/datetime/AddHoursToDateUnitTest.java @@ -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)); + } + +}