java.time.YearMonth.atEndOfMonth()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(108)

本文整理了Java中java.time.YearMonth.atEndOfMonth()方法的一些代码示例,展示了YearMonth.atEndOfMonth()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YearMonth.atEndOfMonth()方法的具体详情如下:
包路径:java.time.YearMonth
类名称:YearMonth
方法名:atEndOfMonth

YearMonth.atEndOfMonth介绍

[英]Returns a LocalDate at the end of the month.

This returns a LocalDate based on this year-month. The day-of-month is set to the last valid day of the month, taking into account leap years.

This method can be used as part of a chain to produce a date:

  1. LocalDate date = year.atMonth(month).atEndOfMonth();

[中]在月底返回LocalDate。
这将返回基于今年月份的LocalDate。考虑到闰年,月日设置为该月的最后一个有效日。
此方法可作为链的一部分用于生成日期:

  1. LocalDate date = year.atMonth(month).atEndOfMonth();

代码示例

代码示例来源:origin: stackoverflow.com

  1. private static final String DATE_PATTERN = "MM/yy";
  2. public int getLastDayOfMonth(String dateString) {
  3. DateTimeFormatter pattern = DateTimeFormatter.ofPattern(DATE_PATTERN);
  4. YearMonth yearMonth = YearMonth.parse(dateString, pattern);
  5. LocalDate date = yearMonth.atEndOfMonth();
  6. return date.lengthOfMonth();
  7. }

代码示例来源:origin: stackoverflow.com

  1. YearMonth ym = YearMonth.of(2012, 1);
  2. String firstDay = ym.atDay(1).getDayOfWeek().name();
  3. String lastDay = ym.atEndOfMonth().getDayOfWeek().name();
  4. System.out.println(firstDay);
  5. System.out.println(lastDay);

代码示例来源:origin: stackoverflow.com

  1. public static int countDayOccurenceInMonth(DayOfWeek dow, YearMonth month) {
  2. LocalDate start = month.atDay(1).with(TemporalAdjusters.nextOrSame(dow));
  3. return (int) ChronoUnit.WEEKS.between(start, month.atEndOfMonth()) + 1;
  4. }

代码示例来源:origin: stackoverflow.com

  1. ZonedId zoneId = ZoneId.of( "America/Montreal" );
  2. YearMonth yearMonthNow = YearMonth.now( zoneId );
  3. YearMonth yearMonthPrevious = yearMonthNow.minusMonths( 1 );
  4. LocalDate firstOfMonth = yearMonthPrevious.atDay( 1 );
  5. LocalDate lastOfMonth = yearMonthPrevious.atEndOfMonth();

代码示例来源:origin: Silverpeas/Silverpeas-Core

  1. /**
  2. * Gets a window of time on this calendar defined by the specified period. The window of time
  3. * will include only the events in this calendar that occur in the specified period.
  4. * @param yearMonth the month and year during which the events in this calendar occur.
  5. * @return the window of time including the events in this calendar occurring in the given period.
  6. */
  7. public CalendarTimeWindow in(final YearMonth yearMonth) {
  8. return between(yearMonth.atDay(1), yearMonth.atEndOfMonth());
  9. }

代码示例来源:origin: stackoverflow.com

  1. YearMonth ym = YearMonth.parse( "2016-12" );
  2. LocalDate firstOfMonth = ym.atDay( 1 );
  3. LocalDate lastOfMonth = ym.atEndOfMonth();
  4. int nthWeek = 1;
  5. LocalDate ld = firstOfMonth;
  6. while( ! ld.isAfter( lastOfMonth ) ) {
  7. System.out.print( ld.getDayOfMonth() + ", " );
  8. if( ld.getDayOfWeek().equals( DayOfWeek.SUNDAY ) ) {
  9. System.out.println( "" ); // Wrap to next line.
  10. }
  11. ld = ld.plusDays( 1 ); // Setup for next loop.
  12. }
  13. System.out.println( "\nDone." );

代码示例来源:origin: Silverpeas/Silverpeas-Core

  1. /**
  2. * Gets a window of time on this calendar defined by the specified period. The window of time
  3. * will include only the events in this calendar that occur in the specified period.
  4. * @param year the year during which the events in this calendar occur.
  5. * @return the window of time including the events in this calendar occurring in the given period.
  6. */
  7. public CalendarTimeWindow in(final Year year) {
  8. return between(year.atDay(1), year.atMonth(DECEMBER).atEndOfMonth());
  9. }

代码示例来源:origin: OpenGamma/Strata

  1. @Override
  2. public double value(PriceIndexObservation observation) {
  3. YearMonth fixingMonth = observation.getFixingMonth();
  4. // If fixing in the past, check time series and returns the historic month price index if present
  5. if (fixingMonth.isBefore(YearMonth.from(valuationDate))) {
  6. OptionalDouble fixing = fixings.get(fixingMonth.atEndOfMonth());
  7. if (fixing.isPresent()) {
  8. return fixing.getAsDouble();
  9. }
  10. }
  11. throw new MarketDataNotFoundException("Unable to query forward value for historic index " + index);
  12. }

代码示例来源:origin: OpenGamma/Strata

  1. @Override
  2. public double value(PriceIndexObservation observation) {
  3. YearMonth fixingMonth = observation.getFixingMonth();
  4. // If fixing in the past, check time series and returns the historic month price index if present
  5. if (fixingMonth.isBefore(YearMonth.from(valuationDate))) {
  6. OptionalDouble fixing = fixings.get(fixingMonth.atEndOfMonth());
  7. if (fixing.isPresent()) {
  8. return fixing.getAsDouble();
  9. }
  10. }
  11. // otherwise, return the estimate from the curve.
  12. double nbMonth = numberOfMonths(fixingMonth);
  13. return curve.yValue(nbMonth);
  14. }

代码示例来源:origin: OpenGamma/Strata

  1. public void test_value() {
  2. for (int i = 0; i < TEST_MONTHS.length; i++) {
  3. double valueComputed = INSTANCE.value(TEST_OBS[i]);
  4. YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
  5. double valueExpected;
  6. if (USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
  7. valueExpected = USCPI_TS.get(fixingMonth.atEndOfMonth()).getAsDouble();
  8. } else {
  9. double x = YearMonth.from(VAL_DATE).until(fixingMonth, MONTHS);
  10. valueExpected = CURVE_INFL.yValue(x);
  11. }
  12. assertEquals(valueComputed, valueExpected, TOLERANCE_VALUE, "test " + i);
  13. }
  14. }

代码示例来源:origin: OpenGamma/Strata

  1. @Override
  2. public PointSensitivityBuilder valuePointSensitivity(PriceIndexObservation observation) {
  3. YearMonth fixingMonth = observation.getFixingMonth();
  4. // If fixing in the past, check time series and returns the historic month price index if present
  5. if (fixingMonth.isBefore(YearMonth.from(valuationDate))) {
  6. if (fixings.get(fixingMonth.atEndOfMonth()).isPresent()) {
  7. return PointSensitivityBuilder.none();
  8. }
  9. }
  10. return InflationRateSensitivity.of(observation, 1d);
  11. }

代码示例来源:origin: OpenGamma/Strata

  1. @Override
  2. public PointSensitivityBuilder valuePointSensitivity(PriceIndexObservation observation) {
  3. YearMonth fixingMonth = observation.getFixingMonth();
  4. // If fixing in the past, check time series and returns the historic month price index if present
  5. if (fixingMonth.isBefore(YearMonth.from(valuationDate))) {
  6. if (fixings.get(fixingMonth.atEndOfMonth()).isPresent()) {
  7. return PointSensitivityBuilder.none();
  8. }
  9. }
  10. throw new MarketDataNotFoundException("Unable to query forward value sensitivity for historic index " + index);
  11. }

代码示例来源:origin: OpenGamma/Strata

  1. private UnitParameterSensitivities unitParameterSensitivity(YearMonth month) {
  2. // If fixing in the past, check time series and returns the historic month price index if present
  3. if (month.isBefore(YearMonth.from(valuationDate))) {
  4. if (fixings.get(month.atEndOfMonth()).isPresent()) {
  5. return UnitParameterSensitivities.empty();
  6. }
  7. }
  8. double nbMonth = numberOfMonths(month);
  9. return UnitParameterSensitivities.of(curve.yValueParameterSensitivity(nbMonth));
  10. }

代码示例来源:origin: OpenGamma/Strata

  1. public void test_value_futfixing() {
  2. for (int i = 0; i < TEST_MONTHS.length; i++) {
  3. double valueComputed = INSTANCE_WITH_FUTFIXING.value(TEST_OBS[i]);
  4. YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
  5. double valueExpected;
  6. if (fixingMonth.isBefore(YearMonth.from(VAL_DATE_2)) && USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
  7. valueExpected = USCPI_TS.get(fixingMonth.atEndOfMonth()).getAsDouble();
  8. } else {
  9. double x = YearMonth.from(VAL_DATE_2).until(fixingMonth, MONTHS);
  10. valueExpected = CURVE_INFL2.yValue(x);
  11. }
  12. assertEquals(valueComputed, valueExpected, TOLERANCE_VALUE, "test " + i);
  13. }
  14. }

代码示例来源:origin: OpenGamma/Strata

  1. public void test_value_pts_sensitivity() {
  2. for (int i = 0; i < TEST_MONTHS.length; i++) {
  3. PointSensitivityBuilder ptsComputed = INSTANCE.valuePointSensitivity(TEST_OBS[i]);
  4. YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
  5. PointSensitivityBuilder ptsExpected;
  6. if (USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
  7. ptsExpected = PointSensitivityBuilder.none();
  8. } else {
  9. ptsExpected = InflationRateSensitivity.of(TEST_OBS[i], 1d);
  10. }
  11. assertTrue(ptsComputed.build().equalWithTolerance(ptsExpected.build(), TOLERANCE_VALUE), "test " + i);
  12. }
  13. }

代码示例来源:origin: OpenGamma/Strata

  1. public void coverage() {
  2. HistoricPriceIndexValues instance1 = HistoricPriceIndexValues.of(US_CPI_U, VAL_DATE, USCPI_TS);
  3. coverImmutableBean(instance1);
  4. HistoricPriceIndexValues test2 =
  5. HistoricPriceIndexValues.of(
  6. GB_HICP,
  7. VAL_DATE.plusMonths(1),
  8. LocalDateDoubleTimeSeries.of(VAL_MONTH.minusMonths(2).atEndOfMonth(), 100d));
  9. coverBeanEquals(instance1, test2);
  10. }

代码示例来源:origin: OpenGamma/Strata

  1. public void coverage() {
  2. SimplePriceIndexValues instance1 = SimplePriceIndexValues.of(US_CPI_U, VAL_DATE, CURVE_NOFIX, USCPI_TS);
  3. coverImmutableBean(instance1);
  4. SimplePriceIndexValues test2 =
  5. SimplePriceIndexValues.of(
  6. GB_HICP,
  7. VAL_DATE.plusMonths(1),
  8. CURVE_NOFIX,
  9. LocalDateDoubleTimeSeries.of(VAL_MONTH.minusMonths(2).atEndOfMonth(), 100d));
  10. coverBeanEquals(instance1, test2);
  11. }

代码示例来源:origin: OpenGamma/Strata

  1. public void test_value_pts_sensitivity_futfixing() {
  2. for (int i = 0; i < TEST_MONTHS.length; i++) {
  3. PointSensitivityBuilder ptsComputed = INSTANCE_WITH_FUTFIXING.valuePointSensitivity(TEST_OBS[i]);
  4. YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
  5. PointSensitivityBuilder ptsExpected;
  6. if (fixingMonth.isBefore(YearMonth.from(VAL_DATE_2)) && USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
  7. ptsExpected = PointSensitivityBuilder.none();
  8. } else {
  9. ptsExpected = InflationRateSensitivity.of(TEST_OBS[i], 1d);
  10. }
  11. assertTrue(ptsComputed.build().equalWithTolerance(ptsExpected.build(), TOLERANCE_VALUE), "test " + i);
  12. }
  13. }

代码示例来源:origin: OpenGamma/Strata

  1. public void test_value_parameter_sensitivity() {
  2. for (int i = 0; i < TEST_MONTHS.length; i++) {
  3. YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
  4. if (!USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
  5. InflationRateSensitivity ptsExpected = (InflationRateSensitivity) InflationRateSensitivity.of(TEST_OBS[i], 1d);
  6. CurrencyParameterSensitivities psComputed = INSTANCE.parameterSensitivity(ptsExpected);
  7. double x = YearMonth.from(VAL_DATE).until(fixingMonth, MONTHS);
  8. UnitParameterSensitivities sens1 = UnitParameterSensitivities.of(CURVE_INFL.yValueParameterSensitivity(x));
  9. CurrencyParameterSensitivities psExpected =
  10. sens1.multipliedBy(ptsExpected.getCurrency(), ptsExpected.getSensitivity());
  11. assertTrue(psComputed.equalWithTolerance(psExpected, TOLERANCE_DELTA), "test " + i);
  12. }
  13. }
  14. }

代码示例来源:origin: OpenGamma/Strata

  1. public void test_value_parameter_sensitivity_futfixing() {
  2. for (int i = 0; i < TEST_MONTHS.length; i++) {
  3. YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
  4. if (!fixingMonth.isBefore(YearMonth.from(VAL_DATE_2)) && !USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
  5. InflationRateSensitivity ptsExpected = (InflationRateSensitivity) InflationRateSensitivity.of(TEST_OBS[i], 1d);
  6. CurrencyParameterSensitivities psComputed = INSTANCE_WITH_FUTFIXING.parameterSensitivity(ptsExpected);
  7. double x = YearMonth.from(VAL_DATE_2).until(fixingMonth, MONTHS);
  8. UnitParameterSensitivities sens1 = UnitParameterSensitivities.of(CURVE_INFL2.yValueParameterSensitivity(x));
  9. CurrencyParameterSensitivities psExpected =
  10. sens1.multipliedBy(ptsExpected.getCurrency(), ptsExpected.getSensitivity());
  11. assertTrue(psComputed.equalWithTolerance(psExpected, TOLERANCE_DELTA), "test " + i);
  12. }
  13. }
  14. }

相关文章