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

The following examples show how to use java.time.LocalDate#toEpochDay() . 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: ForwardIborInterpolatedRateComputationFnTest.java    From Strata with Apache License 2.0 6 votes vote down vote up
@Test
public void test_rateSensitivity() {
  RatesProvider mockProv = mock(RatesProvider.class);
  IborIndexRates mockRates3M = mock(IborIndexRates.class);
  IborIndexRates mockRates6M = mock(IborIndexRates.class);
  when(mockProv.iborIndexRates(GBP_LIBOR_3M)).thenReturn(mockRates3M);
  when(mockProv.iborIndexRates(GBP_LIBOR_6M)).thenReturn(mockRates6M);
  when(mockRates3M.ratePointSensitivity(GBP_LIBOR_3M_OBS)).thenReturn(SENSITIVITY3);
  when(mockRates6M.ratePointSensitivity(GBP_LIBOR_6M_OBS)).thenReturn(SENSITIVITY6);

  IborInterpolatedRateComputation ro = IborInterpolatedRateComputation.of(GBP_LIBOR_3M, GBP_LIBOR_6M, FIXING_DATE, REF_DATA);
  ForwardIborInterpolatedRateComputationFn obsFn = ForwardIborInterpolatedRateComputationFn.DEFAULT;
  LocalDate fixingEndDate3M = GBP_LIBOR_3M_OBS.getMaturityDate();
  LocalDate fixingEndDate6M = GBP_LIBOR_6M_OBS.getMaturityDate();
  double days3M = fixingEndDate3M.toEpochDay() - FIXING_DATE.toEpochDay(); //nb days in 3M fixing period
  double days6M = fixingEndDate6M.toEpochDay() - FIXING_DATE.toEpochDay(); //nb days in 6M fixing period
  double daysCpn = ACCRUAL_END_DATE.toEpochDay() - FIXING_DATE.toEpochDay();
  double weight3M = (days6M - daysCpn) / (days6M - days3M);
  double weight6M = (daysCpn - days3M) / (days6M - days3M);
  IborRateSensitivity sens3 = IborRateSensitivity.of(GBP_LIBOR_3M_OBS, weight3M);
  IborRateSensitivity sens6 = IborRateSensitivity.of(GBP_LIBOR_6M_OBS, weight6M);
  PointSensitivities expected = PointSensitivities.of(ImmutableList.of(sens3, sens6));
  PointSensitivityBuilder test = obsFn.rateSensitivity(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, mockProv);
  assertThat(test.build()).isEqualTo(expected);
}
 
Example 2
Source File: LocalDates.java    From mockneat with Apache License 2.0 6 votes vote down vote up
/**
 * <p>This method can be used to generate date objects in the defined range (lowerDate, upperDate).</p>
 *
 * @param lowerDate The lower limit of the interval.
 * @param upperDate The upper limit of the interval.
 * @return A new {@code MockUnitLocalDate} object.
 */
public MockUnitLocalDate between(LocalDate lowerDate, LocalDate upperDate) {
    notNull(lowerDate, "lowerDate");
    notNull(upperDate, "upperDate");
    isTrue(lowerDate.compareTo(upperDate)<0,
            LOWER_DATE_SMALLER_THAN_UPPER_DATE,
            "lower", lowerDate,
            "upper", upperDate);
    Supplier<LocalDate> supp = () -> {
        long lowerEpoch = lowerDate.toEpochDay();
        long upperEpoch = upperDate.toEpochDay();
        long diff = upperEpoch - lowerEpoch;
        long randEpoch = mockNeat.longs().range(0, diff).val();
        return ofEpochDay(lowerEpoch + randEpoch);
    };
    return ()-> supp;
}
 
Example 3
Source File: TCKLocalDate.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() {
    TEST_2007_07_15 = LocalDate.of(2007, 7, 15);

    LocalDate max = LocalDate.MAX;
    LocalDate min = LocalDate.MIN;
    MAX_VALID_EPOCHDAYS = max.toEpochDay();
    MIN_VALID_EPOCHDAYS = min.toEpochDay();
    MAX_DATE = max;
    MIN_DATE = min;
    MAX_INSTANT = max.atStartOfDay(ZoneOffset.UTC).toInstant();
    MIN_INSTANT = min.atStartOfDay(ZoneOffset.UTC).toInstant();
}
 
Example 4
Source File: TCKLocalDate.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() {
    TEST_2007_07_15 = LocalDate.of(2007, 7, 15);

    LocalDate max = LocalDate.MAX;
    LocalDate min = LocalDate.MIN;
    MAX_VALID_EPOCHDAYS = max.toEpochDay();
    MIN_VALID_EPOCHDAYS = min.toEpochDay();
    MAX_DATE = max;
    MIN_DATE = min;
    MAX_INSTANT = max.atStartOfDay(ZoneOffset.UTC).toInstant();
    MIN_INSTANT = min.atStartOfDay(ZoneOffset.UTC).toInstant();
}
 
Example 5
Source File: TCKLocalDate.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() {
    TEST_2007_07_15 = LocalDate.of(2007, 7, 15);

    LocalDate max = LocalDate.MAX;
    LocalDate min = LocalDate.MIN;
    MAX_VALID_EPOCHDAYS = max.toEpochDay();
    MIN_VALID_EPOCHDAYS = min.toEpochDay();
    MAX_DATE = max;
    MIN_DATE = min;
    MAX_INSTANT = max.atStartOfDay(ZoneOffset.UTC).toInstant();
    MIN_INSTANT = min.atStartOfDay(ZoneOffset.UTC).toInstant();
}
 
Example 6
Source File: BattleLogs.java    From logbook-kai with MIT License 5 votes vote down vote up
/**
 * 任意期間の集計単位
 *
 * @param from 期間の開始日付
 * @param to 期間の終了日付
 */
public CustomUnit(LocalDate from, LocalDate to) {
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
    // 単位の名前
    // yy/MM/dd-yy/MM/dd(xx日)
    this.name = formatter.format(from) + "-" + formatter.format(to)
            + "(" + ((to.toEpochDay() - from.toEpochDay()) + 1) + "日)";
    // 期間の開始
    this.from = ZonedDateTime.of(LocalDateTime.of(from, LocalTime.MIN), ZoneId.of("GMT+04:00"));
    // 期間の終了
    this.to = ZonedDateTime.of(LocalDateTime.of(to, LocalTime.MAX), ZoneId.of("GMT+04:00"));
}
 
Example 7
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();
}
 
Example 8
Source File: TCKLocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() {
    TEST_2007_07_15 = LocalDate.of(2007, 7, 15);

    LocalDate max = LocalDate.MAX;
    LocalDate min = LocalDate.MIN;
    MAX_VALID_EPOCHDAYS = max.toEpochDay();
    MIN_VALID_EPOCHDAYS = min.toEpochDay();
    MAX_DATE = max;
    MIN_DATE = min;
    MAX_INSTANT = max.atStartOfDay(ZoneOffset.UTC).toInstant();
    MIN_INSTANT = min.atStartOfDay(ZoneOffset.UTC).toInstant();
}
 
Example 9
Source File: TCKLocalDate.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() {
    TEST_2007_07_15 = LocalDate.of(2007, 7, 15);

    LocalDate max = LocalDate.MAX;
    LocalDate min = LocalDate.MIN;
    MAX_VALID_EPOCHDAYS = max.toEpochDay();
    MIN_VALID_EPOCHDAYS = min.toEpochDay();
    MAX_DATE = max;
    MIN_DATE = min;
    MAX_INSTANT = max.atStartOfDay(ZoneOffset.UTC).toInstant();
    MIN_INSTANT = min.atStartOfDay(ZoneOffset.UTC).toInstant();
}
 
Example 10
Source File: Core.java    From DataDefender with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a random date between the passed start and end dates, and using
 * the passed format to parse the dates passed, and to format the return
 * value.
 *
 * @param start
 * @param end
 * @param format
 * @return
 */
public String randomDate(
    @NamedParameter("start") String start,
    @NamedParameter("end") String end,
    @NamedParameter("format") String format
) {
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(format);
    LocalDate ds = LocalDate.parse(start, fmt);
    LocalDate de = LocalDate.parse(end, fmt);
    long day = RandomUtils.nextLong(0, de.toEpochDay() - ds.toEpochDay()) + ds.toEpochDay();
    return LocalDate.ofEpochDay(day).format(fmt);
}
 
Example 11
Source File: QueueStatistics.java    From rqueue with Apache License 2.0 5 votes vote down vote up
public void pruneStats(LocalDate date, int maxDays) {
  if (startEpochDate == null) {
    startEpochDate = date.toEpochDay();
    return;
  }
  long daysDifference = date.toEpochDay() - startEpochDate;
  long extraDaysData = daysDifference - maxDays;
  if (extraDaysData > 0) {
    LocalDate startDate = date.minusDays(maxDays);
    this.startEpochDate = startDate.toEpochDay();
    for (int i = 0; i <= extraDaysData; i++) {
      cleanData(startDate.minusDays(i).toString());
    }
  }
}
 
Example 12
Source File: GreetingResource.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@GET
@Produces(MediaType.APPLICATION_JSON)
public Greeting hello() {
    LocalDate localDate = LocalDate.of(2019, 01, 01);
    return new Greeting("hello", localDate, new Date(localDate.toEpochDay()));
}
 
Example 13
Source File: JsonRowDataDeserializationSchema.java    From flink with Apache License 2.0 4 votes vote down vote up
private int convertToDate(JsonNode jsonNode) {
	LocalDate date = ISO_LOCAL_DATE.parse(jsonNode.asText()).query(TemporalQueries.localDate());
	return (int) date.toEpochDay();
}
 
Example 14
Source File: LongCoding.java    From morpheus-core with Apache License 2.0 4 votes vote down vote up
@Override
public final long getCode(LocalDate value) {
    return value == null ? Long.MIN_VALUE : value.toEpochDay();
}
 
Example 15
Source File: ForwardIborInterpolatedRateComputationFnTest.java    From Strata with Apache License 2.0 4 votes vote down vote up
@Test
public void test_rate() {
  RatesProvider mockProv = mock(RatesProvider.class);
  LocalDateDoubleTimeSeries timeSeries = LocalDateDoubleTimeSeries.of(FIXING_DATE, RATE3TS);
  IborIndexRates mockRates3M = new TestingIborIndexRates(
      GBP_LIBOR_3M, FIXING_DATE, LocalDateDoubleTimeSeries.empty(), timeSeries);
  IborIndexRates mockRates6M = new TestingIborIndexRates(
      GBP_LIBOR_6M, FIXING_DATE, LocalDateDoubleTimeSeries.of(FIXING_DATE, RATE6), LocalDateDoubleTimeSeries.empty());
  when(mockProv.iborIndexRates(GBP_LIBOR_3M)).thenReturn(mockRates3M);
  when(mockProv.iborIndexRates(GBP_LIBOR_6M)).thenReturn(mockRates6M);

  IborInterpolatedRateComputation ro = IborInterpolatedRateComputation.of(GBP_LIBOR_3M, GBP_LIBOR_6M, FIXING_DATE, REF_DATA);
  ForwardIborInterpolatedRateComputationFn obs = ForwardIborInterpolatedRateComputationFn.DEFAULT;
  LocalDate fixingEndDate3M = GBP_LIBOR_3M_OBS.getMaturityDate();
  LocalDate fixingEndDate6M = GBP_LIBOR_6M_OBS.getMaturityDate();
  double days3M = fixingEndDate3M.toEpochDay() - FIXING_DATE.toEpochDay(); //nb days in 3M fixing period
  double days6M = fixingEndDate6M.toEpochDay() - FIXING_DATE.toEpochDay(); //nb days in 6M fixing period
  double daysCpn = ACCRUAL_END_DATE.toEpochDay() - FIXING_DATE.toEpochDay();
  double weight3M = (days6M - daysCpn) / (days6M - days3M);
  double weight6M = (daysCpn - days3M) / (days6M - days3M);
  double rateExpected = (weight3M * RATE3TS + weight6M * RATE6);
  double rateComputed = obs.rate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, mockProv);
  assertThat(rateComputed).isCloseTo(rateExpected, offset(TOLERANCE_RATE));

  // explain
  ExplainMapBuilder builder = ExplainMap.builder();
  assertThat(obs.explainRate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, mockProv, builder)).isCloseTo(rateExpected, offset(TOLERANCE_RATE));

  ExplainMap built = builder.build();
  assertThat(built.get(ExplainKey.OBSERVATIONS)).isPresent();
  assertThat(built.get(ExplainKey.OBSERVATIONS).get()).hasSize(2);
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.FIXING_DATE)).isEqualTo(Optional.of(FIXING_DATE));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.INDEX)).isEqualTo(Optional.of(GBP_LIBOR_3M));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.INDEX_VALUE)).isEqualTo(Optional.of(RATE3TS));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.WEIGHT)).isEqualTo(Optional.of(weight3M));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.FROM_FIXING_SERIES)).isEqualTo(Optional.of(Boolean.TRUE));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.FIXING_DATE)).isEqualTo(Optional.of(FIXING_DATE));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.INDEX)).isEqualTo(Optional.of(GBP_LIBOR_6M));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.INDEX_VALUE)).isEqualTo(Optional.of(RATE6));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.WEIGHT)).isEqualTo(Optional.of(weight6M));
  assertThat(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.FROM_FIXING_SERIES)).isEqualTo(Optional.empty());
  assertThat(built.get(ExplainKey.COMBINED_RATE)).isEqualTo(Optional.of(rateExpected));
}
 
Example 16
Source File: Date.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public Long toBaseType(LocalDate input) {
  return input == null ? null : input.toEpochDay();
}
 
Example 17
Source File: ThriftMetastoreUtil.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Date toMetastoreDate(LocalDate date)
{
    return new Date(date.toEpochDay());
}
 
Example 18
Source File: DayCountConvention_ACT.java    From finmath-lib with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the number of days, between two dates.
 *
 * A fractional day is rounded to the approximately nearest day.
 *
 * The formula implemented is
 * <code>
 *
 * (endDate.toEpochDay() - startDate.toEpochDay());
 * </code>
 *
 * @param startDate The start date of the interval.
 * @param endDate The end date of the interval.
 * @return Number of days between startDate and endDate.
 */
public static double daysBetween(final LocalDate startDate, final LocalDate endDate) {
	return (endDate.toEpochDay() - startDate.toEpochDay());
}
 
Example 19
Source File: DayCountConvention_ACT.java    From finmath-lib with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the number of days, between two dates.
 *
 * A fractional day is rounded to the approximately nearest day.
 *
 * The formula implemented is
 * <code>
 *
 * (endDate.toEpochDay() - startDate.toEpochDay());
 * </code>
 *
 * @param startDate The start date of the interval.
 * @param endDate The end date of the interval.
 * @return Number of days between startDate and endDate.
 */
public static double daysBetween(final LocalDate startDate, final LocalDate endDate) {
	return (endDate.toEpochDay() - startDate.toEpochDay());
}
 
Example 20
Source File: DateUtils.java    From yue-library with Apache License 2.0 2 votes vote down vote up
/**
 * 计算日期相差天数
 * 
 * @param startDate 开始日期
 * @param endDate 结束日期
 * @return 相差天数
 */
public static long dateDaysDifference(LocalDate startDate, LocalDate endDate) {
	return startDate.toEpochDay() - endDate.toEpochDay();
}