BAEL-5465: tests for subtracting days from date (#11954)
* BAEL-5465: tests for subtracting days from date * BAEL-5465: upated readme
This commit is contained in:
parent
8f7a2b7662
commit
4ab85e1ccc
|
@ -11,4 +11,5 @@ This module contains articles about date operations in Java.
|
|||
- [How to determine day of week by passing specific date in Java?](https://www.baeldung.com/java-get-day-of-week)
|
||||
- [Finding Leap Years in Java](https://www.baeldung.com/java-leap-year)
|
||||
- [Getting the Week Number From Any Date](https://www.baeldung.com/java-get-week-number)
|
||||
- [Subtract Days from a Date in Java](https://www.baeldung.com/java-subtract-days-from-a-date)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-date-operations-1)
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.subtractdays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SubtractDaysFromDateUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCalendarDate_whenSubtractingFiveDays_dateIsChangedCorrectly() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2022, Calendar.APRIL, 20);
|
||||
|
||||
calendar.add(Calendar.DATE, -5);
|
||||
|
||||
assertEquals(15, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(Calendar.APRIL, calendar.get(Calendar.MONTH));
|
||||
assertEquals(2022, calendar.get(Calendar.YEAR));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJodaDateTime_whenSubtractingFiveDays_dateIsChangedCorrectly() {
|
||||
DateTime dateTime = new DateTime(2022, 4, 20, 12, 0, 0);
|
||||
|
||||
dateTime = dateTime.minusDays(5);
|
||||
|
||||
assertEquals(15, dateTime.getDayOfMonth());
|
||||
assertEquals(4, dateTime.getMonthOfYear());
|
||||
assertEquals(2022, dateTime.getYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTime_whenSubtractingFiveDays_dateIsChangedCorrectly() {
|
||||
LocalDate localDateTime = LocalDate.of(2022, 4, 20);
|
||||
|
||||
localDateTime = localDateTime.minusDays(5);
|
||||
|
||||
assertEquals(15, localDateTime.getDayOfMonth());
|
||||
assertEquals(4, localDateTime.getMonthValue());
|
||||
assertEquals(2022, localDateTime.getYear());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue