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

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

本文整理了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:

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

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

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

代码示例

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

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();
}

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

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);

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

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;
}

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

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();

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

/**
 * 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());
}

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

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." );

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

/**
 * 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());
}

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

@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);
}

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

@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);
}

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

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);
 }
}

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

@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);
}

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

@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);
}

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

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));
}

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

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);
 }
}

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

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);
 }
}

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

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);
}

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

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);
}

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

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);
 }
}

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

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);
  }
 }
}

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

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);
  }
 }
}

相关文章