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

The following examples show how to use java.time.LocalDate#lengthOfYear() . 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: SingleExecutionTime.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
private Optional<TimeNode> generateDayCandidatesUsingDoY(final ZonedDateTime reference) {
    final int year = reference.getYear();
    final int month = reference.getMonthValue();
    final LocalDate date = LocalDate.of(year, 1, 1);
    final int lengthOfYear = date.lengthOfYear();

    final List<Integer> candidates = createDayOfYearValueGeneratorInstance(daysOfYearCronField, year).generateCandidates(1, lengthOfYear);

    final int low = LocalDate.of(year, month, 1).getDayOfYear();
    final int high = month == 12
            ? LocalDate.of(year, 12, 31).getDayOfYear() + 1
            : LocalDate.of(year, month + 1, 1).getDayOfYear();

    final List<Integer> collectedCandidates = candidates.stream().filter(dayOfYear -> dayOfYear >= low && dayOfYear < high)
            .map(dayOfYear -> LocalDate.ofYearDay(reference.getYear(), dayOfYear).getDayOfMonth())
            .collect(Collectors.toList());

    return Optional.of(collectedCandidates).filter(not(List::isEmpty)).map(TimeNode::new);
}
 
Example 2
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_hubu")
public void test_hubu(int year, List<LocalDate> holidays, List<LocalDate> workDays) {
  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) &&
        !workDays.contains(date);
    assertThat(HUBU.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 3
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_nzwe")
public void test_nzwe(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(NZWE.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 4
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_dkco")
public void test_dkco(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(DKCO.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 5
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_czpr")
public void test_czpr(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(CZPR.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 6
Source File: DayCountConvention_ACT_ACT_ISDA.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
@Override
public double getDaycountFraction(final LocalDate startDate, final LocalDate endDate) {
	if(startDate.isAfter(endDate)) {
		return -getDaycountFraction(endDate,startDate);
	}

	/*
	 * Number of whole years between start and end, excluding start's year and excluding end's year.
	 *
	 * If start and end fall in the same year, this is -1 (there will be a double counting of 1 year below if start < end).
	 * If start and end fall in separate but consecutive years, this is 0 (for start and end the fractional parts are counted individually below).
	 */
	double daycountFraction = endDate.getYear() - startDate.getYear() - 1.0;

	/*
	 * Fraction from start to the end of start's year
	 */
	LocalDate startDateNextYear = LocalDate.of(startDate.getYear()+1,Month.JANUARY,1);

	if(isCountLastDayNotFirst) {
		startDateNextYear = startDateNextYear.minusDays(1);
	}

	daycountFraction += getDaycount(startDate, startDateNextYear) / startDate.lengthOfYear();

	/*
	 * Fraction from beginning of end's year to end
	 */
	LocalDate endDateStartYear = LocalDate.of(endDate.getYear(), Month.JANUARY, 1);
	if (isCountLastDayNotFirst) {
		endDateStartYear = endDateStartYear.minusDays(1);
	}


	daycountFraction += getDaycount(endDateStartYear, endDate) / endDate.lengthOfYear();

	return Math.max(daycountFraction,0.0);
}
 
Example 7
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 8
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_brbd")
public void test_brbd(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(BRBD.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 9
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_ausy")
public void test_ausy(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(AUSY.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 10
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_nzau")
public void test_nzau(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(NZAU.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 11
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_nyse")
public void test_nyse(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(NYSE.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 12
Source File: DayCountConvention_ACT_ACT_ISDA.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
@Override
public double getDaycountFraction(final LocalDate startDate, final LocalDate endDate) {
	if(startDate.isAfter(endDate)) {
		return -getDaycountFraction(endDate,startDate);
	}

	/*
	 * Number of whole years between start and end, excluding start's year and excluding end's year.
	 *
	 * If start and end fall in the same year, this is -1 (there will be a double counting of 1 year below if start < end).
	 * If start and end fall in separate but consecutive years, this is 0 (for start and end the fractional parts are counted individually below).
	 */
	double daycountFraction = endDate.getYear() - startDate.getYear() - 1.0;

	/*
	 * Fraction from start to the end of start's year
	 */
	LocalDate startDateNextYear = LocalDate.of(startDate.getYear()+1,Month.JANUARY,1);

	if(isCountLastDayNotFirst) {
		startDateNextYear = startDateNextYear.minusDays(1);
	}

	daycountFraction += getDaycount(startDate, startDateNextYear) / startDate.lengthOfYear();

	/*
	 * Fraction from beginning of end's year to end
	 */
	LocalDate endDateStartYear = LocalDate.of(endDate.getYear(), Month.JANUARY, 1);
	if (isCountLastDayNotFirst) {
		endDateStartYear = endDateStartYear.minusDays(1);
	}


	daycountFraction += getDaycount(endDateStartYear, endDate) / endDate.lengthOfYear();

	return Math.max(daycountFraction,0.0);
}
 
Example 13
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_usny")
public void test_usny(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(USNY.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 14
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_usgs")
public void test_usgs(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(USGS.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 15
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_euta")
public void test_euta(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(EUTA.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 16
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_chzu")
public void test_chzu(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(CHZU.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 17
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_noos")
public void test_noos(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(NOOS.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 18
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_frpa")
public void test_frpa(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(FRPA.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 19
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_gblo")
public void test_gblo(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(GBLO.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 20
Source File: LocalDateUtils.java    From Strata with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the number of days between two dates.
 * <p>
 * Faster than the JDK method.
 * 
 * @param firstDate  the first date
 * @param secondDate  the second date, after the first
 * @return the new date
 */
static long daysBetween(LocalDate firstDate, LocalDate secondDate) {
  int firstYear = firstDate.getYear();
  int secondYear = secondDate.getYear();
  if (firstYear == secondYear) {
    return doy(secondDate) - doy(firstDate);
  }
  if ((firstYear + 1) == secondYear) {
    return (firstDate.lengthOfYear() - doy(firstDate)) + doy(secondDate);
  }
  return secondDate.toEpochDay() - firstDate.toEpochDay();
}