Skip to content

Commit faf6b79

Browse files
authored
BAEL-6397: Adding One Month to Current Date in Java (#14261)
1 parent c54937d commit faf6b79

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

core-java-modules/core-java-date-operations-3/pom.xml

+18
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,23 @@
1212
<artifactId>core-java-modules</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>joda-time</groupId>
19+
<artifactId>joda-time</artifactId>
20+
<version>${joda-time.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.apache.commons</groupId>
24+
<artifactId>commons-lang3</artifactId>
25+
<version>${commons-lang3.version}</version>
26+
</dependency>
27+
</dependencies>
28+
29+
<properties>
30+
<joda-time.version>2.12.5</joda-time.version>
31+
<commons-lang3.version>3.12.0</commons-lang3.version>
32+
</properties>
1533

1634
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.date;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.time.LocalDate;
6+
import java.util.Calendar;
7+
import java.util.Date;
8+
import java.util.GregorianCalendar;
9+
10+
import org.apache.commons.lang3.time.DateUtils;
11+
import org.junit.jupiter.api.Test;
12+
13+
class AddOneMonthToCurrentDateUnitTest {
14+
15+
@Test
16+
void givenCalendarDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
17+
Calendar calendar = Calendar.getInstance();
18+
// Dummy current date
19+
calendar.set(2023, Calendar.APRIL, 20);
20+
21+
// add one month
22+
calendar.add(Calendar.MONTH, 1);
23+
24+
assertEquals(Calendar.MAY, calendar.get(Calendar.MONTH));
25+
assertEquals(20, calendar.get(Calendar.DAY_OF_MONTH));
26+
assertEquals(2023, calendar.get(Calendar.YEAR));
27+
}
28+
29+
@SuppressWarnings("deprecation")
30+
@Test
31+
void givenDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
32+
// Dummy current date
33+
Date currentDate = new Date(2023, Calendar.DECEMBER, 20);
34+
// Expected Date
35+
Date expectedDate = new Date(2024, Calendar.JANUARY, 20);
36+
37+
// add one month
38+
currentDate.setMonth(currentDate.getMonth() + 1);
39+
40+
assertEquals(expectedDate, currentDate);
41+
}
42+
43+
@Test
44+
void givenJavaLocalDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
45+
// Dummy current date
46+
LocalDate localDate = LocalDate.of(2023, 12, 20);
47+
48+
// add one month
49+
localDate = localDate.plusMonths(1);
50+
51+
assertEquals(1, localDate.getMonthValue());
52+
assertEquals(20, localDate.getDayOfMonth());
53+
assertEquals(2024, localDate.getYear());
54+
}
55+
56+
@Test
57+
void givenJodaTimeLocalDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
58+
// Dummy current date
59+
org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(2023, 12, 20);
60+
61+
// add one month
62+
localDate = localDate.plusMonths(1);
63+
64+
assertEquals(1, localDate.getMonthOfYear());
65+
assertEquals(20, localDate.getDayOfMonth());
66+
assertEquals(2024, localDate.getYear());
67+
}
68+
69+
@Test
70+
void givenApacheCommonsLangDateUtils_whenAddingOneMonth_thenDateIsChangedCorrectly() {
71+
// Dummy current date
72+
Date currentDate = new GregorianCalendar(2023, Calendar.APRIL, 20, 4, 0).getTime();
73+
// Expected Date
74+
Date expectedDate = new GregorianCalendar(2023, Calendar.MAY, 20, 4, 0).getTime();
75+
76+
assertEquals(expectedDate, DateUtils.addMonths(currentDate, 1));
77+
}
78+
79+
}

0 commit comments

Comments
 (0)