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

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

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

YearMonth.from介绍

[英]Obtains an instance of YearMonth from a temporal object.

A TemporalAccessor represents some form of date and time information. This factory converts the arbitrary temporal object to an instance of YearMonth.

The conversion extracts the ChronoField#YEAR and ChronoField#MONTH_OF_YEAR fields. The extraction is only permitted if the temporal object has an ISO chronology, or can be converted to a LocalDate.

This method matches the signature of the functional interface TemporalQueryallowing it to be used in queries via method reference, YearMonth::from.
[中]从时间对象获取YearMonth的实例。
临时助理代表某种形式的日期和时间信息。该工厂将任意时态对象转换为YearMonth的实例。
转换将提取ChronoField#YEAR和ChronoField#MONTH_OF_YEAR字段。仅当时态对象具有ISO年表或可以转换为LocalDate时,才允许提取。
此方法匹配函数接口TemporalQueryLow的签名,通过方法引用YearMonth::from将其用于查询。

代码示例

代码示例来源:origin: jfoenixadmin/JFoenix

  1. void clearFocus() {
  2. LocalDate focusDate = datePicker.getValue();
  3. if (focusDate == null) {
  4. focusDate = LocalDate.now();
  5. }
  6. if (YearMonth.from(focusDate).equals(selectedYearMonth.get())) {
  7. goToDate(focusDate, true);
  8. }
  9. }

代码示例来源:origin: jfoenixadmin/JFoenix

  1. private void goToDate(LocalDate date, boolean focusDayCell) {
  2. if (isValidDate(datePicker.getChronology(), date)) {
  3. selectedYearMonth.set(YearMonth.from(date));
  4. if (focusDayCell) {
  5. findDayCellOfDate(date).requestFocus();
  6. }
  7. }
  8. }

代码示例来源:origin: jfoenixadmin/JFoenix

  1. YearMonth.from(date) : YearMonth.now());
  2. content.updateValues();
  3. LocalDate date = jfxDatePicker.getValue();
  4. content.displayedYearMonthProperty().set((date != null) ?
  5. YearMonth.from(date) : YearMonth.now());
  6. content.updateValues();

代码示例来源:origin: jfoenixadmin/JFoenix

  1. selectedYearMonth.set((date != null) ? YearMonth.from(date) : YearMonth.now());
  2. selectedYearMonth.addListener((observable, oldValue, newValue) -> updateValues());

代码示例来源:origin: com.github.seratch/java-time-backport

  1. @Override
  2. public YearMonth queryFrom(TemporalAccessor temporal) {
  3. return YearMonth.from(temporal);
  4. }
  5. };

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

  1. @Override
  2. public DatedParameterMetadata metadata(LocalDate valuationDate, ReferenceData refData) {
  3. LocalDate nodeDate = date(valuationDate, refData);
  4. LocalDate referenceDate = template.calculateReferenceDateFromTradeDate(valuationDate, refData);
  5. if (label.isEmpty()) {
  6. return YearMonthDateParameterMetadata.of(nodeDate, YearMonth.from(referenceDate));
  7. }
  8. return YearMonthDateParameterMetadata.of(nodeDate, YearMonth.from(referenceDate), label);
  9. }

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

  1. @Override
  2. public YearMonth convertToEntityAttribute(java.sql.Date dbData) {
  3. return YearMonth.from(Instant.ofEpochMilli(dbData.getTime())
  4. .atZone(ZoneId.systemDefault())
  5. .toLocalDate());
  6. }
  7. }

代码示例来源:origin: com.jfoenix/jfoenix

  1. void clearFocus() {
  2. LocalDate focusDate = datePicker.getValue();
  3. if (focusDate == null) {
  4. focusDate = LocalDate.now();
  5. }
  6. if (YearMonth.from(focusDate).equals(selectedYearMonth.get())) {
  7. goToDate(focusDate, true);
  8. }
  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: com.jfoenix/jfoenix

  1. private void goToDate(LocalDate date, boolean focusDayCell) {
  2. if (isValidDate(datePicker.getChronology(), date)) {
  3. selectedYearMonth.set(YearMonth.from(date));
  4. if (focusDayCell) {
  5. findDayCellOfDate(date).requestFocus();
  6. }
  7. }
  8. }

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

  1. @Test(dataProvider = "quarterly10th")
  2. public void test_nextOrSameQuarterly10th(LocalDate base, LocalDate expect1, LocalDate expect2, LocalDate expect3) {
  3. LocalDate date = base.plusDays(1);
  4. while (!date.isAfter(expect1)) {
  5. assertEquals(DateSequences.QUARTERLY_10TH.nextOrSame(date), expect1);
  6. assertEquals(DateSequences.QUARTERLY_10TH.nthOrSame(date, 1), expect1);
  7. assertEquals(DateSequences.QUARTERLY_10TH.nthOrSame(date, 2), expect2);
  8. assertEquals(DateSequences.QUARTERLY_10TH.nthOrSame(date, 3), expect3);
  9. date = date.plusDays(1);
  10. }
  11. assertEquals(DateSequences.QUARTERLY_10TH.dateMatching(YearMonth.from(date)), expect1);
  12. }

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

  1. @Test(dataProvider = "monthlyImm")
  2. public void test_nextOrSameMonthlyImm(LocalDate base, LocalDate immDate1, LocalDate immDate2, LocalDate immDate3) {
  3. LocalDate date = base.plusDays(1);
  4. while (!date.isAfter(immDate1)) {
  5. assertEquals(DateSequences.MONTHLY_IMM.nextOrSame(date), immDate1);
  6. assertEquals(DateSequences.MONTHLY_IMM.nthOrSame(date, 1), immDate1);
  7. assertEquals(DateSequences.MONTHLY_IMM.nthOrSame(date, 2), immDate2);
  8. assertEquals(DateSequences.MONTHLY_IMM.nthOrSame(date, 3), immDate3);
  9. date = date.plusDays(1);
  10. }
  11. assertEquals(DateSequences.MONTHLY_IMM.dateMatching(YearMonth.from(date)), immDate1);
  12. }

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

  1. @Test(dataProvider = "quarterlyImm")
  2. public void test_nextOrSameQuarterlyImm(LocalDate base, LocalDate immDate1, LocalDate immDate2, LocalDate immDate3) {
  3. LocalDate date = base.plusDays(1);
  4. while (!date.isAfter(immDate1)) {
  5. assertEquals(DateSequences.QUARTERLY_IMM.nextOrSame(date), immDate1);
  6. assertEquals(DateSequences.QUARTERLY_IMM.nthOrSame(date, 1), immDate1);
  7. assertEquals(DateSequences.QUARTERLY_IMM.nthOrSame(date, 2), immDate2);
  8. assertEquals(DateSequences.QUARTERLY_IMM.nthOrSame(date, 3), immDate3);
  9. date = date.plusDays(1);
  10. }
  11. assertEquals(DateSequences.QUARTERLY_IMM.dateMatching(YearMonth.from(date)), immDate1);
  12. }

代码示例来源: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. @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. public void test_metadata_end() {
  2. IborFutureCurveNode node = IborFutureCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD, LABEL);
  3. LocalDate date = LocalDate.of(2015, 10, 20);
  4. LocalDate referenceDate = TEMPLATE.calculateReferenceDateFromTradeDate(date, REF_DATA);
  5. LocalDate maturityDate = TEMPLATE.getIndex().calculateMaturityFromEffective(referenceDate, REF_DATA);
  6. ParameterMetadata metadata = node.metadata(date, REF_DATA);
  7. assertEquals(metadata.getLabel(), LABEL);
  8. assertTrue(metadata instanceof YearMonthDateParameterMetadata);
  9. assertEquals(((YearMonthDateParameterMetadata) metadata).getDate(), maturityDate);
  10. assertEquals(((YearMonthDateParameterMetadata) metadata).getYearMonth(), YearMonth.from(referenceDate));
  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 value_multiplicative() {
  2. InflationNodalCurve curveComputed = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
  3. SEASONALITY_MULTIPLICATIVE_DEF);
  4. for (int i = 1; i < TEST_MONTHS.length; i++) {
  5. double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
  6. double valueComputed = curveComputed.yValue(nbMonths);
  7. int x = (int) ((nbMonths + 12) % 12);
  8. double valueNoAdj = EXTENDED_CURVE_2.yValue(nbMonths);
  9. double adj = SEASONALITY_MULTIPLICATIVE_COMP_2.get(x);
  10. double valueExpected = valueNoAdj * adj;
  11. assertEquals(valueExpected, valueComputed, TOLERANCE_VALUE);
  12. }
  13. }

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

  1. public void test_metadata_last_fixing() {
  2. IborFutureCurveNode node =
  3. IborFutureCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD, LABEL).withDate(CurveNodeDate.LAST_FIXING);
  4. ImmutableMarketData marketData = ImmutableMarketData.builder(VAL_DATE).addValue(QUOTE_ID, 0.0d).build();
  5. IborFutureTrade trade = node.trade(1d, marketData, REF_DATA);
  6. LocalDate fixingDate = trade.getProduct().getFixingDate();
  7. DatedParameterMetadata metadata = node.metadata(VAL_DATE, REF_DATA);
  8. assertEquals(metadata.getDate(), fixingDate);
  9. LocalDate referenceDate = TEMPLATE.calculateReferenceDateFromTradeDate(VAL_DATE, REF_DATA);
  10. assertEquals(((YearMonthDateParameterMetadata) metadata).getYearMonth(), YearMonth.from(referenceDate));
  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. }

相关文章