Skip to content

Commit 387d93e

Browse files
shubhi22KevinGilmore
authored andcommitted
BAEL-1901 Add hours to Date in Java (eugenp#4599)
* BAEL-1901 Add hours to Date in Java * BAEL-1901 : Editor Review Changes * BAEL-1901 using assertThat for assertions
1 parent e5f2714 commit 387d93e

2 files changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.Duration;
4+
import java.time.Instant;
5+
import java.time.LocalDateTime;
6+
import java.time.ZonedDateTime;
7+
import java.time.temporal.ChronoUnit;
8+
import java.util.Calendar;
9+
import java.util.Date;
10+
11+
import org.apache.commons.lang3.time.DateUtils;
12+
13+
public class AddHoursToDate {
14+
15+
public Date addHoursToJavaUtilDate(Date date, int hours) {
16+
Calendar calendar = Calendar.getInstance();
17+
calendar.setTime(date);
18+
calendar.add(Calendar.HOUR_OF_DAY, hours);
19+
return calendar.getTime();
20+
}
21+
22+
public Date addHoursToDateUsingInstant(Date date, int hours) {
23+
return Date.from(date.toInstant()
24+
.plus(Duration.ofHours(hours)));
25+
}
26+
27+
public LocalDateTime addHoursToLocalDateTime(LocalDateTime localDateTime, int hours) {
28+
return localDateTime.plusHours(hours);
29+
}
30+
31+
public LocalDateTime subtractHoursToLocalDateTime(LocalDateTime localDateTime, int hours) {
32+
return localDateTime.minusHours(hours);
33+
}
34+
35+
public ZonedDateTime addHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
36+
return zonedDateTime.plusHours(hours);
37+
}
38+
39+
public ZonedDateTime subtractHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
40+
return zonedDateTime.minusHours(hours);
41+
}
42+
43+
public Instant addHoursToInstant(Instant instant, int hours) {
44+
return instant.plus(hours, ChronoUnit.HOURS);
45+
}
46+
47+
public Instant subtractHoursToInstant(Instant instant, int hours) {
48+
return instant.minus(hours, ChronoUnit.HOURS);
49+
}
50+
51+
public Date addHoursWithApacheCommons(Date date, int hours) {
52+
return DateUtils.addHours(date, hours);
53+
}
54+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.baeldung.datetime;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.time.Instant;
6+
import java.time.LocalDateTime;
7+
import java.time.Month;
8+
import java.time.ZoneId;
9+
import java.time.ZonedDateTime;
10+
import java.util.Calendar;
11+
import java.util.Date;
12+
import java.util.GregorianCalendar;
13+
14+
import org.junit.Test;
15+
16+
public class AddHoursToDateUnitTest {
17+
18+
private final AddHoursToDate addHoursToDateObj = new AddHoursToDate();
19+
20+
@Test
21+
public void givenJavaUtilDate_whenPositiveHours_thenAddHours() {
22+
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
23+
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 7, 0).getTime();
24+
25+
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursToJavaUtilDate(actualDate, 2));
26+
}
27+
28+
@Test
29+
public void givenJavaUtilDate_whenNegativeHours_thenMinusHours() {
30+
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
31+
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime();
32+
33+
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursToJavaUtilDate(actualDate, -2));
34+
}
35+
36+
@Test
37+
public void givenJavaUtilDate_whenUsingToInstantAndPostiveHours_thenAddHours() {
38+
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
39+
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 7, 0).getTime();
40+
41+
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursToDateUsingInstant(actualDate, 2));
42+
}
43+
44+
@Test
45+
public void givenJavaUtilDate_whenUsingToInstantAndNegativeHours_thenAddHours() {
46+
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
47+
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime();
48+
49+
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursToDateUsingInstant(actualDate, -2));
50+
}
51+
52+
@Test
53+
public void givenLocalDateTime_whenUsingAddHoursToLocalDateTime_thenAddHours() {
54+
LocalDateTime actualDateTime = LocalDateTime.of(2018, Month.JUNE, 25, 5, 0);
55+
LocalDateTime expectedDateTime = LocalDateTime.of(2018, Month.JUNE, 25, 7, 0);
56+
57+
assertThat(expectedDateTime).isEqualTo(addHoursToDateObj.addHoursToLocalDateTime(actualDateTime, 2));
58+
}
59+
60+
@Test
61+
public void givenLocalDateTime_whenUsingMinusHoursToLocalDateTime_thenMinusHours() {
62+
LocalDateTime actualDateTime = LocalDateTime.of(2018, Month.JUNE, 25, 5, 0);
63+
LocalDateTime expectedDateTime = LocalDateTime.of(2018, Month.JUNE, 25, 3, 0);
64+
65+
assertThat(expectedDateTime).isEqualTo(addHoursToDateObj.subtractHoursToLocalDateTime(actualDateTime, 2));
66+
}
67+
68+
@Test
69+
public void givenZonedDateTime_whenUsingAddHoursToZonedDateTime_thenAddHours() {
70+
ZonedDateTime actualZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2018, Month.JUNE, 25, 5, 0), ZoneId.systemDefault());
71+
ZonedDateTime expectedZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2018, Month.JUNE, 25, 7, 0), ZoneId.systemDefault());
72+
73+
assertThat(expectedZonedDateTime).isEqualTo(addHoursToDateObj.addHoursToZonedDateTime(actualZonedDateTime, 2));
74+
}
75+
76+
@Test
77+
public void givenZonedDateTime_whenUsingMinusHoursToZonedDateTime_thenMinusHours() {
78+
ZonedDateTime actualZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2018, Month.JUNE, 25, 5, 0), ZoneId.systemDefault());
79+
ZonedDateTime expectedZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2018, Month.JUNE, 25, 3, 0), ZoneId.systemDefault());
80+
81+
assertThat(expectedZonedDateTime).isEqualTo(addHoursToDateObj.subtractHoursToZonedDateTime(actualZonedDateTime, 2));
82+
}
83+
84+
@Test
85+
public void givenJavaUtilDate_whenUsingPositiveHrsAndAddHoursWithApacheCommons_thenAddHours() {
86+
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
87+
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 7, 0).getTime();
88+
89+
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursWithApacheCommons(actualDate, 2));
90+
}
91+
92+
@Test
93+
public void givenJavaUtilDate_whenUsingNegativeHrsAndAddHoursWithApacheCommons_thenMinusHours() {
94+
Date actualDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 7, 0).getTime();
95+
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 5, 0).getTime();
96+
97+
assertThat(expectedDate).isEqualTo(addHoursToDateObj.addHoursWithApacheCommons(actualDate, -2));
98+
}
99+
100+
@Test
101+
public void givenInstant_whenUsingAddHoursToInstant_thenAddHours() {
102+
Instant actualValue = Instant.parse("2018-06-25T05:12:35Z");
103+
Instant expectedValue = Instant.parse("2018-06-25T07:12:35Z");
104+
105+
assertThat(expectedValue).isEqualTo(addHoursToDateObj.addHoursToInstant(actualValue, 2));
106+
}
107+
108+
@Test
109+
public void givenInstant_whenUsingSubtractHoursToInstant_thenMinusHours() {
110+
Instant actualValue = Instant.parse("2018-06-25T07:12:35Z");
111+
Instant expectedValue = Instant.parse("2018-06-25T05:12:35Z");
112+
113+
assertThat(expectedValue).isEqualTo(addHoursToDateObj.subtractHoursToInstant(actualValue, 2));
114+
}
115+
116+
}

0 commit comments

Comments
 (0)