BAEL-6397: Adding One Month to Current Date in Java (#14261)
This commit is contained in:
parent
c54937de56
commit
faf6b79e86
|
@ -12,5 +12,23 @@
|
|||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.12.5</joda-time.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,79 @@
|
|||
package com.baeldung.date;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AddOneMonthToCurrentDateUnitTest {
|
||||
|
||||
@Test
|
||||
void givenCalendarDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
// Dummy current date
|
||||
calendar.set(2023, Calendar.APRIL, 20);
|
||||
|
||||
// add one month
|
||||
calendar.add(Calendar.MONTH, 1);
|
||||
|
||||
assertEquals(Calendar.MAY, calendar.get(Calendar.MONTH));
|
||||
assertEquals(20, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(2023, calendar.get(Calendar.YEAR));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
void givenDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
|
||||
// Dummy current date
|
||||
Date currentDate = new Date(2023, Calendar.DECEMBER, 20);
|
||||
// Expected Date
|
||||
Date expectedDate = new Date(2024, Calendar.JANUARY, 20);
|
||||
|
||||
// add one month
|
||||
currentDate.setMonth(currentDate.getMonth() + 1);
|
||||
|
||||
assertEquals(expectedDate, currentDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJavaLocalDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
|
||||
// Dummy current date
|
||||
LocalDate localDate = LocalDate.of(2023, 12, 20);
|
||||
|
||||
// add one month
|
||||
localDate = localDate.plusMonths(1);
|
||||
|
||||
assertEquals(1, localDate.getMonthValue());
|
||||
assertEquals(20, localDate.getDayOfMonth());
|
||||
assertEquals(2024, localDate.getYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJodaTimeLocalDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
|
||||
// Dummy current date
|
||||
org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(2023, 12, 20);
|
||||
|
||||
// add one month
|
||||
localDate = localDate.plusMonths(1);
|
||||
|
||||
assertEquals(1, localDate.getMonthOfYear());
|
||||
assertEquals(20, localDate.getDayOfMonth());
|
||||
assertEquals(2024, localDate.getYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenApacheCommonsLangDateUtils_whenAddingOneMonth_thenDateIsChangedCorrectly() {
|
||||
// Dummy current date
|
||||
Date currentDate = new GregorianCalendar(2023, Calendar.APRIL, 20, 4, 0).getTime();
|
||||
// Expected Date
|
||||
Date expectedDate = new GregorianCalendar(2023, Calendar.MAY, 20, 4, 0).getTime();
|
||||
|
||||
assertEquals(expectedDate, DateUtils.addMonths(currentDate, 1));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue