Java Code Examples for java.time.LocalDate#plusDays()

The following examples show how to use java.time.LocalDate#plusDays() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TCKLocalDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_plusHours_fromOne() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(22, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);

        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 2
Source File: TCKWeekFields.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoWBYDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(yowbyField).appendLiteral(':')
                .appendValue(wowbyField).appendLiteral(':')
                .appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
        int wowby = date.get(wowbyField);
        int dow = date.get(dowField);
        for (int j = wowby - 60; j < wowby + 60; j++) {
            String str = date.get(yowbyField) + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wowby), " ::" + str + ": :" + i + "::" + j);
        }

        date = date.plusDays(1);
    }
}
 
Example 3
Source File: TCKWeekFields.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("data_weekFields")
public void test_parse_resolve_localizedWoy_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField woyField = week.weekOfYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(YEAR).appendLiteral(':')
                .appendValue(woyField).appendLiteral(':')
                .appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
        int woy = date.get(woyField);
        int dow = date.get(DAY_OF_WEEK);
        for (int j = woy - 60; j < woy + 60; j++) {
            String str = date.getYear() + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(" ::" + str + ": :" + i + "::" + j, parsed, date.plusWeeks(j - woy));
        }

        date = date.plusDays(1);
    }
}
 
Example 4
Source File: TCKLocalDateTime.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void test_plusHours_fromOne() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(22, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);

        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 5
Source File: GlobalHolidayCalendars.java    From Strata with Apache License 2.0 6 votes vote down vote up
private static LocalDate bumpSunToMon(LocalDate date) {
  if (date.getDayOfWeek() == SUNDAY) {
    return date.plusDays(1);
  }
  return date;
}
 
Example 6
Source File: TCKLocalDateTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_plusHours_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(21, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);
        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 7
Source File: TCKWeekFields.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoWBY(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(yowbyField).appendLiteral(':')
                .appendValue(wowbyField).appendLiteral(':')
                .appendValue(DAY_OF_WEEK).toFormatter();
        String str = date.get(yowbyField) + ":" + date.get(wowbyField) + ":" +
                date.get(DAY_OF_WEEK);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(parsed, date, " :: " + str + " " + i);

        date = date.plusDays(1);
    }
}
 
Example 8
Source File: TCKWeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoWBYDow(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(yowbyField).appendLiteral(':')
                .appendValue(wowbyField).appendLiteral(':')
                .appendValue(dowField).toFormatter();
        String str = date.get(yowbyField) + ":" + date.get(wowbyField) + ":" +
                date.get(dowField);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(parsed, date, " :: " + str + " " + i);

        date = date.plusDays(1);
    }
}
 
Example 9
Source File: TCKLocalDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_plusMinutes_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(1);
    LocalTime t = LocalTime.of(22, 49);

    for (int i = -70; i < 70; i++) {
        LocalDateTime dt = base.plusMinutes(i);
        t = t.plusMinutes(1);

        if (t.equals(LocalTime.MIDNIGHT)) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d, String.valueOf(i));
        assertEquals(dt.toLocalTime(), t, String.valueOf(i));
    }
}
 
Example 10
Source File: DepositTest.java    From finmath-lib with Apache License 2.0 6 votes vote down vote up
public DepositTest() {
	dc = DiscountCurveInterpolation.createDiscountCurveFromDiscountFactors(dcName, times, dcFactors);

	// set up model
	model = new AnalyticModelFromCurvesAndVols(new Curve[] { dc });

	// set up deposit
	final LocalDate refDate = LocalDate.of(2017, 1, 1);
	final LocalDate startDate = refDate.plusDays(2);
	final LocalDate maturity = startDate.plusDays(180);
	sched = ScheduleGenerator.createScheduleFromConventions(refDate, startDate, maturity,
			ScheduleGenerator.Frequency.ANNUAL, ScheduleGenerator.DaycountConvention.ACT_360,
			ShortPeriodConvention.FIRST, DateRollConvention.UNADJUSTED,
			new BusinessdayCalendarAny(), -2, 0);

}
 
Example 11
Source File: TestIsoWeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "fields")
public void test_getFrom(TemporalField weekField, TemporalField yearField) {
    // tests every day from 2011 to 2016 inclusive
    LocalDate date = LocalDate.of(2011, 1, 3);
    int wby = 2011;
    int week = 1;
    int dow = 1;
    for (int i = 1; i <= ((52 + 52 + 52 + 52 + 53 + 52) * 7); i++) {
        assertEquals(yearField.getFrom(date), wby);
        assertEquals(weekField.getFrom(date), week);
        assertEquals(DAY_OF_WEEK.getFrom(date), dow);
        if (dow == 7) {
            dow = 1;
            week++;
        } else {
            dow++;
        }
        if (week > wbyLen(wby)) {
            week = 1;
            wby++;
        }
        date = date.plusDays(1);
    }
    assertEquals(yearField.getFrom(date), 2017);
    assertEquals(weekField.getFrom(date), 1);
    assertEquals(DAY_OF_WEEK.getFrom(date), 1);
}
 
Example 12
Source File: TestIsoWeekFields.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "fields")
public void test_adjustInto_dow(TemporalField weekField, TemporalField yearField) {
    // tests every day from 2012 to 2016 inclusive
    LocalDate date = LocalDate.of(2012, 1, 2);
    int wby = 2012;
    int week = 1;
    int dow = 1;
    for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
        for (int j = 1; j <= 7; j++) {
            LocalDate adjusted = DAY_OF_WEEK.adjustInto(date, j);
            assertEquals(adjusted.get(DAY_OF_WEEK), j);
            assertEquals(adjusted.get(weekField), week);
            assertEquals(adjusted.get(yearField), wby);
        }
        if (dow == 7) {
            dow = 1;
            week++;
        } else {
            dow++;
        }
        if (week > wbyLen(wby)) {
            week = 1;
            wby++;
        }
        date = date.plusDays(1);
    }
}
 
Example 13
Source File: TCKYear.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_atDay_leapYear() {
    Year test = Year.of(2008);
    LocalDate expected = LocalDate.of(2008, 1, 1);
    for (int i = 1; i <= 366; i++) {
        assertEquals(test.atDay(i), expected);
        expected = expected.plusDays(1);
    }
}
 
Example 14
Source File: DenseLocalDateDoubleTimeSeries.java    From Strata with Apache License 2.0 5 votes vote down vote up
@Override
LocalDate calculateDateFromPosition(LocalDate startDate, int position) {
  int numWeekends = position / 5;
  int remaining = position % 5;
  // As above we add adjustment for an uncaptured weekend
  int endPointAdjustment = (remaining < (6 - startDate.get(DAY_OF_WEEK))) ? 0 : 2;
  return startDate.plusDays((7 * numWeekends) + remaining + endPointAdjustment);
}
 
Example 15
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_camo")
public void test_camo(int year, List<LocalDate> holidays) {
  LocalDate date = LocalDate.of(year, 1, 1);
  int len = date.lengthOfYear();
  for (int i = 0; i < len; i++) {
    boolean isHoliday = holidays.contains(date) || date.getDayOfWeek() == SATURDAY || date.getDayOfWeek() == SUNDAY;
    assertThat(CAMO.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 16
Source File: TCKIsoFields.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void test_loop() {
    // loop round at least one 400 year cycle, including before 1970
    LocalDate date = LocalDate.of(1960, 1, 5);  // Tuseday of week 1 1960
    int year = 1960;
    int wby = 1960;
    int weekLen = 52;
    int week = 1;
    while (date.getYear() < 2400) {
        DayOfWeek loopDow = date.getDayOfWeek();
        if (date.getYear() != year) {
            year = date.getYear();
        }
        if (loopDow == MONDAY) {
            week++;
            if ((week == 53 && weekLen == 52) || week == 54) {
                week = 1;
                LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1);
                DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek();
                weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52);
                wby++;
            }
        }
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek());
        date = date.plusDays(1);
    }
}
 
Example 17
Source File: TestIsoWeekFields.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "fields")
public void test_adjustInto_week(TemporalField weekField, TemporalField yearField) {
    // tests every day from 2012 to 2016 inclusive
    LocalDate date = LocalDate.of(2012, 1, 2);
    int wby = 2012;
    int week = 1;
    int dow = 1;
    for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
        int weeksInYear = (wby == 2015 ? 53 : 52);
        for (int j = 1; j <= weeksInYear; j++) {
            LocalDate adjusted = weekField.adjustInto(date, j);
            assertEquals(adjusted.get(weekField), j);
            assertEquals(adjusted.get(DAY_OF_WEEK), dow);
            assertEquals(adjusted.get(yearField), wby);
        }
        if (dow == 7) {
            dow = 1;
            week++;
        } else {
            dow++;
        }
        if (week > wbyLen(wby)) {
            week = 1;
            wby++;
        }
        date = date.plusDays(1);
    }
}
 
Example 18
Source File: TCKWeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify that the date can be reconstructed from the DOW, WeekOfWeekBasedYear,
 * and WeekBasedYear for every combination of start of week
 * and minimal days in week.
 * @param firstDayOfWeek the first day of the week
 * @param minDays the minimum number of days in the week
 */
@Test(dataProvider="weekFields")
public void test_weekOfWeekBasedYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOWBY = day.get(wowbyField);
        int actualYOWBY = day.get(yowbyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = LocalDate.of(actualYOWBY, 1, 1);
        DayOfWeek isoDOW = day1.getDayOfWeek();
        int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;

        int weekStart = Math.floorMod(1 - dow, 7);
        if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
            // The previous week has the minimum days in the current month to be a 'week'
            weekStart -= 7;
        }
        weekStart += actualDOW - 1;
        weekStart += (actualWOWBY - 1) * 7;
        LocalDate result = day1.plusDays(weekStart);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, weekStart: %s, actualDOW: %s, actualWOWBY: %s, YearOfWBY: %d, expected day: %s, result: %s%n",
                weekDef, day.getDayOfWeek(), weekStart, actualDOW, actualWOWBY, actualYOWBY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 19
Source File: LocalDateUtilsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@Test
public void test_plusDays1() {
  LocalDate date = LocalDate.of(2012, 1, 1);
  for (int i = 0; i < 366 * 4; i++) {
    assertThat(LocalDateUtils.plusDays(date, 1)).isEqualTo(date.plusDays(1));
    date = date.plusDays(1);
  }
}
 
Example 20
Source File: TCKLocalDate.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="PlusDays")
public void test_plusDays_normal(LocalDate input, int amountsToAdd, LocalDate expected) {
    LocalDate actual = input.plusDays(amountsToAdd);
    assertEquals(actual, expected);
 }