private static final String DATE_PATTERN = "MM/yy"; public int getLastDayOfMonth(String dateString) { DateTimeFormatter pattern = DateTimeFormatter.ofPattern(DATE_PATTERN); YearMonth yearMonth = YearMonth.parse(dateString, pattern); LocalDate date = yearMonth.atEndOfMonth(); return date.lengthOfMonth(); }
YearMonth ym = YearMonth.of(2012, 1); String firstDay = ym.atDay(1).getDayOfWeek().name(); String lastDay = ym.atEndOfMonth().getDayOfWeek().name(); System.out.println(firstDay); System.out.println(lastDay);
public static int countDayOccurenceInMonth(DayOfWeek dow, YearMonth month) { LocalDate start = month.atDay(1).with(TemporalAdjusters.nextOrSame(dow)); return (int) ChronoUnit.WEEKS.between(start, month.atEndOfMonth()) + 1; }
ZonedId zoneId = ZoneId.of( "America/Montreal" ); YearMonth yearMonthNow = YearMonth.now( zoneId ); YearMonth yearMonthPrevious = yearMonthNow.minusMonths( 1 ); LocalDate firstOfMonth = yearMonthPrevious.atDay( 1 ); LocalDate lastOfMonth = yearMonthPrevious.atEndOfMonth();
/** * Gets a window of time on this calendar defined by the specified period. The window of time * will include only the events in this calendar that occur in the specified period. * @param yearMonth the month and year during which the events in this calendar occur. * @return the window of time including the events in this calendar occurring in the given period. */ public CalendarTimeWindow in(final YearMonth yearMonth) { return between(yearMonth.atDay(1), yearMonth.atEndOfMonth()); }
YearMonth ym = YearMonth.parse( "2016-12" ); LocalDate firstOfMonth = ym.atDay( 1 ); LocalDate lastOfMonth = ym.atEndOfMonth(); int nthWeek = 1; LocalDate ld = firstOfMonth; while( ! ld.isAfter( lastOfMonth ) ) { System.out.print( ld.getDayOfMonth() + ", " ); if( ld.getDayOfWeek().equals( DayOfWeek.SUNDAY ) ) { System.out.println( "" ); // Wrap to next line. } ld = ld.plusDays( 1 ); // Setup for next loop. } System.out.println( "\nDone." );
/** * Gets a window of time on this calendar defined by the specified period. The window of time * will include only the events in this calendar that occur in the specified period. * @param year the year during which the events in this calendar occur. * @return the window of time including the events in this calendar occurring in the given period. */ public CalendarTimeWindow in(final Year year) { return between(year.atDay(1), year.atMonth(DECEMBER).atEndOfMonth()); }
@Override public double value(PriceIndexObservation observation) { YearMonth fixingMonth = observation.getFixingMonth(); // If fixing in the past, check time series and returns the historic month price index if present if (fixingMonth.isBefore(YearMonth.from(valuationDate))) { OptionalDouble fixing = fixings.get(fixingMonth.atEndOfMonth()); if (fixing.isPresent()) { return fixing.getAsDouble(); } } throw new MarketDataNotFoundException("Unable to query forward value for historic index " + index); }
@Override public double value(PriceIndexObservation observation) { YearMonth fixingMonth = observation.getFixingMonth(); // If fixing in the past, check time series and returns the historic month price index if present if (fixingMonth.isBefore(YearMonth.from(valuationDate))) { OptionalDouble fixing = fixings.get(fixingMonth.atEndOfMonth()); if (fixing.isPresent()) { return fixing.getAsDouble(); } } // otherwise, return the estimate from the curve. double nbMonth = numberOfMonths(fixingMonth); return curve.yValue(nbMonth); }
public void test_value() { for (int i = 0; i < TEST_MONTHS.length; i++) { double valueComputed = INSTANCE.value(TEST_OBS[i]); YearMonth fixingMonth = TEST_OBS[i].getFixingMonth(); double valueExpected; if (USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) { valueExpected = USCPI_TS.get(fixingMonth.atEndOfMonth()).getAsDouble(); } else { double x = YearMonth.from(VAL_DATE).until(fixingMonth, MONTHS); valueExpected = CURVE_INFL.yValue(x); } assertEquals(valueComputed, valueExpected, TOLERANCE_VALUE, "test " + i); } }
@Override public PointSensitivityBuilder valuePointSensitivity(PriceIndexObservation observation) { YearMonth fixingMonth = observation.getFixingMonth(); // If fixing in the past, check time series and returns the historic month price index if present if (fixingMonth.isBefore(YearMonth.from(valuationDate))) { if (fixings.get(fixingMonth.atEndOfMonth()).isPresent()) { return PointSensitivityBuilder.none(); } } return InflationRateSensitivity.of(observation, 1d); }
@Override public PointSensitivityBuilder valuePointSensitivity(PriceIndexObservation observation) { YearMonth fixingMonth = observation.getFixingMonth(); // If fixing in the past, check time series and returns the historic month price index if present if (fixingMonth.isBefore(YearMonth.from(valuationDate))) { if (fixings.get(fixingMonth.atEndOfMonth()).isPresent()) { return PointSensitivityBuilder.none(); } } throw new MarketDataNotFoundException("Unable to query forward value sensitivity for historic index " + index); }
private UnitParameterSensitivities unitParameterSensitivity(YearMonth month) { // If fixing in the past, check time series and returns the historic month price index if present if (month.isBefore(YearMonth.from(valuationDate))) { if (fixings.get(month.atEndOfMonth()).isPresent()) { return UnitParameterSensitivities.empty(); } } double nbMonth = numberOfMonths(month); return UnitParameterSensitivities.of(curve.yValueParameterSensitivity(nbMonth)); }
public void test_value_futfixing() { for (int i = 0; i < TEST_MONTHS.length; i++) { double valueComputed = INSTANCE_WITH_FUTFIXING.value(TEST_OBS[i]); YearMonth fixingMonth = TEST_OBS[i].getFixingMonth(); double valueExpected; if (fixingMonth.isBefore(YearMonth.from(VAL_DATE_2)) && USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) { valueExpected = USCPI_TS.get(fixingMonth.atEndOfMonth()).getAsDouble(); } else { double x = YearMonth.from(VAL_DATE_2).until(fixingMonth, MONTHS); valueExpected = CURVE_INFL2.yValue(x); } assertEquals(valueComputed, valueExpected, TOLERANCE_VALUE, "test " + i); } }
public void test_value_pts_sensitivity() { for (int i = 0; i < TEST_MONTHS.length; i++) { PointSensitivityBuilder ptsComputed = INSTANCE.valuePointSensitivity(TEST_OBS[i]); YearMonth fixingMonth = TEST_OBS[i].getFixingMonth(); PointSensitivityBuilder ptsExpected; if (USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) { ptsExpected = PointSensitivityBuilder.none(); } else { ptsExpected = InflationRateSensitivity.of(TEST_OBS[i], 1d); } assertTrue(ptsComputed.build().equalWithTolerance(ptsExpected.build(), TOLERANCE_VALUE), "test " + i); } }
public void coverage() { HistoricPriceIndexValues instance1 = HistoricPriceIndexValues.of(US_CPI_U, VAL_DATE, USCPI_TS); coverImmutableBean(instance1); HistoricPriceIndexValues test2 = HistoricPriceIndexValues.of( GB_HICP, VAL_DATE.plusMonths(1), LocalDateDoubleTimeSeries.of(VAL_MONTH.minusMonths(2).atEndOfMonth(), 100d)); coverBeanEquals(instance1, test2); }
public void coverage() { SimplePriceIndexValues instance1 = SimplePriceIndexValues.of(US_CPI_U, VAL_DATE, CURVE_NOFIX, USCPI_TS); coverImmutableBean(instance1); SimplePriceIndexValues test2 = SimplePriceIndexValues.of( GB_HICP, VAL_DATE.plusMonths(1), CURVE_NOFIX, LocalDateDoubleTimeSeries.of(VAL_MONTH.minusMonths(2).atEndOfMonth(), 100d)); coverBeanEquals(instance1, test2); }
public void test_value_pts_sensitivity_futfixing() { for (int i = 0; i < TEST_MONTHS.length; i++) { PointSensitivityBuilder ptsComputed = INSTANCE_WITH_FUTFIXING.valuePointSensitivity(TEST_OBS[i]); YearMonth fixingMonth = TEST_OBS[i].getFixingMonth(); PointSensitivityBuilder ptsExpected; if (fixingMonth.isBefore(YearMonth.from(VAL_DATE_2)) && USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) { ptsExpected = PointSensitivityBuilder.none(); } else { ptsExpected = InflationRateSensitivity.of(TEST_OBS[i], 1d); } assertTrue(ptsComputed.build().equalWithTolerance(ptsExpected.build(), TOLERANCE_VALUE), "test " + i); } }
public void test_value_parameter_sensitivity() { for (int i = 0; i < TEST_MONTHS.length; i++) { YearMonth fixingMonth = TEST_OBS[i].getFixingMonth(); if (!USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) { InflationRateSensitivity ptsExpected = (InflationRateSensitivity) InflationRateSensitivity.of(TEST_OBS[i], 1d); CurrencyParameterSensitivities psComputed = INSTANCE.parameterSensitivity(ptsExpected); double x = YearMonth.from(VAL_DATE).until(fixingMonth, MONTHS); UnitParameterSensitivities sens1 = UnitParameterSensitivities.of(CURVE_INFL.yValueParameterSensitivity(x)); CurrencyParameterSensitivities psExpected = sens1.multipliedBy(ptsExpected.getCurrency(), ptsExpected.getSensitivity()); assertTrue(psComputed.equalWithTolerance(psExpected, TOLERANCE_DELTA), "test " + i); } } }
public void test_value_parameter_sensitivity_futfixing() { for (int i = 0; i < TEST_MONTHS.length; i++) { YearMonth fixingMonth = TEST_OBS[i].getFixingMonth(); if (!fixingMonth.isBefore(YearMonth.from(VAL_DATE_2)) && !USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) { InflationRateSensitivity ptsExpected = (InflationRateSensitivity) InflationRateSensitivity.of(TEST_OBS[i], 1d); CurrencyParameterSensitivities psComputed = INSTANCE_WITH_FUTFIXING.parameterSensitivity(ptsExpected); double x = YearMonth.from(VAL_DATE_2).until(fixingMonth, MONTHS); UnitParameterSensitivities sens1 = UnitParameterSensitivities.of(CURVE_INFL2.yValueParameterSensitivity(x)); CurrencyParameterSensitivities psExpected = sens1.multipliedBy(ptsExpected.getCurrency(), ptsExpected.getSensitivity()); assertTrue(psComputed.equalWithTolerance(psExpected, TOLERANCE_DELTA), "test " + i); } } }