gpt4 book ai didi

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

转载 作者:知者 更新时间:2024-03-18 00:14:40 28 4
gpt4 key购买 nike

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

YearMonth.until介绍

[英]Calculates the period between this year-month and another year-month in terms of the specified unit.

This calculates the period between two year-months in terms of a single unit. The start and end points are this and the specified year-month. The result will be negative if the end is before the start. The Temporal passed to this method must be a YearMonth. For example, the period in years between two year-months can be calculated using startYearMonth.until(endYearMonth, YEARS).

The calculation returns a whole number, representing the number of complete units between the two year-months. For example, the period in decades between 2012-06 and 2032-05 will only be one decade as it is one month short of two decades.

This method operates in association with TemporalUnit#between. The result of this method is a long representing the amount of the specified unit. By contrast, the result of between is an object that can be used directly in addition/subtraction:

long period = start.until(end, YEARS);   // this method 
dateTime.plus(YEARS.between(start, end));      // use in plus/minus

The calculation is implemented in this method for ChronoUnit. The units MONTHS, YEARS, DECADES, CENTURIES, MILLENNIA and ERAS are supported. Other ChronoUnit values will throw an exception.

If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.between(Temporal, Temporal)passing this as the first argument and the input temporal as the second argument.

This instance is immutable and unaffected by this method call.
[中]根据指定的单位计算本年月份和另一年月份之间的期间。
这将根据单个单元计算两个月之间的时间。开始点和结束点为本月和指定的年-月。如果结束早于开始,结果将为负值。传递给此方法的时间必须为一年一个月。例如,可以使用startYearMonth计算两个年月之间的年周期。直到(年底月,年)。
计算结果返回一个整数,表示两年内的完整单元数。例如,从2012-06年到2032-05年的几十年期间将只有十年,因为距离二十年只差一个月。
该方法与TemporalUnit#between结合使用。此方法的结果是一个长的字符,表示指定单位的数量。相比之下,between的结果是一个可以直接用于加法/减法的对象:

long period = start.until(end, YEARS);   // this method 
dateTime.plus(YEARS.between(start, end));      // use in plus/minus

该计算是在ChronoUnit的这种方法中实现的。支持单位月、年、数十年、世纪、千年和年代。其他ChronoUnit值将引发异常。
如果该单元不是ChronoUnit,则通过调用TemporalUnit获得该方法的结果。(时态,时态)将其作为第一个参数传递,将输入时态作为第二个参数传递。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

public static final long getMonthsDifference(Date date1, Date date2) {
  YearMonth m1 = YearMonth.from(date1.toInstant());
  YearMonth m2 = YearMonth.from(date2.toInstant());

  return m1.until(m2, ChronoUnit.MONTHS) + 1;
}

代码示例来源:origin: owlike/genson

private static long getEpochMonth(YearMonth yearMonth){
  return EPOCH_YEAR_MONTH.until(yearMonth, ChronoUnit.MONTHS);
}

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

public static void main(String[] args) {
  test("June, 1999", "April, 2002");
}
private static void test(String fromMonthYear, String toMonthYear) {
  DateTimeFormatter monthYearFormat = DateTimeFormatter.ofPattern("MMMM, uuuu");
  YearMonth from = YearMonth.parse(fromMonthYear, monthYearFormat);
  YearMonth to   = YearMonth.parse(toMonthYear  , monthYearFormat);
  long months = from.until(to, ChronoUnit.MONTHS);
  System.out.printf("%d years and %d months%n", months / 12, months % 12);
}

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

private double numberOfMonths(YearMonth month) {
 return YearMonth.from(valuationDate).until(month, MONTHS);
}

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

private DoubleArray buildNodeTimes(LocalDate valuationDate, CurveMetadata metadata) {
 if (metadata.getXValueType().equals(ValueType.YEAR_FRACTION)) {
  return DoubleArray.of(getParameterCount(), i -> {
   LocalDate nodeDate = ((DatedParameterMetadata) metadata.getParameterMetadata().get().get(i)).getDate();
   return getDayCount().get().yearFraction(valuationDate, nodeDate);
  });
 } else if (metadata.getXValueType().equals(ValueType.MONTHS)) {
  return DoubleArray.of(getParameterCount(), i -> {
   LocalDate nodeDate = ((DatedParameterMetadata) metadata.getParameterMetadata().get().get(i)).getDate();
   return YearMonth.from(valuationDate).until(YearMonth.from(nodeDate), MONTHS);
  });
 } else {
  throw new IllegalArgumentException("Metadata XValueType should be YearFraction or Months in curve definition");
 }
}

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

public void value_multiplicative() {
 InflationNodalCurve curveComputed = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
   SEASONALITY_MULTIPLICATIVE_DEF);
 for (int i = 1; i < TEST_MONTHS.length; i++) {
  double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
  double valueComputed = curveComputed.yValue(nbMonths);
  int x = (int) ((nbMonths + 12) % 12);
  double valueNoAdj = EXTENDED_CURVE_2.yValue(nbMonths);
  double adj = SEASONALITY_MULTIPLICATIVE_COMP_2.get(x);
  double valueExpected = valueNoAdj * adj;
  assertEquals(valueExpected, valueComputed, TOLERANCE_VALUE);
 }
}

代码示例来源: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

public void value_additive() {
 InflationNodalCurve curveComputed = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
   SEASONALITY_ADDITIVE_DEF);
 for (int i = 1; i < TEST_MONTHS.length; i++) {
  double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
  double valueComputed = curveComputed.yValue(nbMonths);
  int x = (int) ((nbMonths + 12) % 12);
  double valueNoAdj = EXTENDED_CURVE_2.yValue(nbMonths);
  DoubleArray seasonalityAdditiveCompounded =
    seasonalityCompounded(VAL_DATE_2, LAST_FIX_MONTH_2, SEASONALITY_ADDITIVE, (v, a) -> v + a);
  double adj = seasonalityAdditiveCompounded.get(x);
  double valueExpected = valueNoAdj + adj;
  assertEquals(valueExpected, valueComputed, TOLERANCE_VALUE);
 }
}

代码示例来源: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

private static DoubleArray seasonalityCompounded(
  LocalDate valDate, YearMonth fixingMonth, DoubleArray seasonality,
  DoubleBinaryOperator adjustmentFunction) {
 double nbMonths = YearMonth.from(valDate).until(fixingMonth, MONTHS);
 double[] seasonalityCompoundedArray = new double[12];
 int lastMonthIndex = fixingMonth.getMonth().getValue() - 2;
 seasonalityCompoundedArray[(int) ((nbMonths + 12 + 1) % 12)] =
   seasonality.get((lastMonthIndex + 1) % 12);
 for (int i = 1; i < 12; i++) {
  int j = (int) ((nbMonths + 12 + 1 + i) % 12);
  seasonalityCompoundedArray[j] = adjustmentFunction.applyAsDouble(
    seasonalityCompoundedArray[(j - 1 + 12) % 12], seasonality.get((lastMonthIndex + 1 + i) % 12));
 }
 return DoubleArray.ofUnsafe(seasonalityCompoundedArray);
}

代码示例来源: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 parameter_sensitivity_additive() {
 InflationNodalCurve curve = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
   SEASONALITY_ADDITIVE_DEF);
 double shift = 1.0E-2;
 for (int i = 1; i < TEST_MONTHS.length; i++) {
  double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
  UnitParameterSensitivity psComputed = curve.yValueParameterSensitivity(nbMonths);
  for (int j = 0; j < TIMES.size(); j++) {
   double[] valuePM = new double[2];
   for (int pm = 0; pm < 2; pm++) {
    DoubleArray shiftedValues = VALUES.with(j, VALUES.get(j) + (1 - 2 * pm) * shift);
    InterpolatedNodalCurve intCurveShifted = InterpolatedNodalCurve.of(METADATA, TIMES, shiftedValues, INTERPOLATOR);
    InflationNodalCurve seaCurveShifted =
      InflationNodalCurve.of(intCurveShifted, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
        SEASONALITY_ADDITIVE_DEF);
    valuePM[pm] = seaCurveShifted.yValue(nbMonths);
   }
   assertEquals(psComputed.getSensitivity().get(j), (valuePM[0] - valuePM[1]) / (2 * shift), TOLERANCE_DELTA);
  }
 }
}

代码示例来源: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);
  }
 }
}

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

public void parameter_sensitivity_multiplicative() {
 InflationNodalCurve curve = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
   SEASONALITY_MULTIPLICATIVE_DEF);
 double shift = 1.0E-2;
 for (int i = 1; i < TEST_MONTHS.length; i++) {
  double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
  UnitParameterSensitivity psComputed = curve.yValueParameterSensitivity(nbMonths);
  for (int j = 0; j < TIMES.size(); j++) {
   double[] valuePM = new double[2];
   for (int pm = 0; pm < 2; pm++) {
    DoubleArray shiftedValues = VALUES.with(j, VALUES.get(j) + (1 - 2 * pm) * shift);
    InterpolatedNodalCurve intCurveShifted = InterpolatedNodalCurve.of(METADATA, TIMES, shiftedValues, INTERPOLATOR);
    InflationNodalCurve seaCurveShifted =
      InflationNodalCurve.of(intCurveShifted, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
        SEASONALITY_MULTIPLICATIVE_DEF);
    valuePM[pm] = seaCurveShifted.yValue(nbMonths);
   }
   assertEquals(psComputed.getSensitivity().get(j), (valuePM[0] - valuePM[1]) / (2 * shift), TOLERANCE_DELTA);
  }
 }
}

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

double nbMonth = valuationMonth.until(lastMonth, MONTHS);
DoubleArray x = curveWithoutFixing.getXValues();
ArgChecker.isTrue(nbMonth < x.get(0), "The first estimation month should be after the last known index fixing");

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com