|
| 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