Java Code Examples for java.time.YearMonth#from()

The following examples show how to use java.time.YearMonth#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YearMonthTypeDescriptor.java    From hibernate-types with Apache License 2.0 6 votes vote down vote up
@Override
public <X> YearMonth wrap(X value, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (value instanceof String) {
        return fromString((String) value);
    }
    if (value instanceof Number) {
        int numericValue = ((Number) (value)).intValue();
        int year = numericValue / 100;
        int month = numericValue % 100;
        return YearMonth.of(year, month);
    }
    if (value instanceof Date) {
        Date date = (Date) value;
        return YearMonth.from(Instant.ofEpochMilli(date.getTime())
                .atZone(ZoneId.systemDefault())
                .toLocalDate());
    }
    throw unknownWrap(value.getClass());
}
 
Example 2
Source File: FHIRPathUtil.java    From FHIR with Apache License 2.0 6 votes vote down vote up
public static TemporalAccessor getTemporalAccessor(Temporal temporal, Class<?> targetType) {
    if (temporal.getClass().equals(targetType)) {
        return temporal;
    }
    if (Year.class.equals(targetType)) {
        return Year.from(temporal);
    } else if (YearMonth.class.equals(targetType)) {
        return YearMonth.from(temporal);
    } else if (LocalDate.class.equals(targetType)) {
        return LocalDate.from(temporal);
    } else if (LocalDateTime.class.equals(targetType)) {
        return LocalDateTime.from(temporal);
    } else if (ZonedDateTime.class.equals(targetType)){
        return ZonedDateTime.from(temporal);
    } else if (LocalTime.class.equals(targetType)) {
        return LocalTime.from(temporal);
    }
    throw new IllegalArgumentException();
}
 
Example 3
Source File: InflationRateCalculation.java    From Strata with Apache License 2.0 6 votes vote down vote up
private RateComputation createRateComputation(SchedulePeriod period, int scheduleIndex) {

    // handle where index value at start date is known
    LocalDate endDate = period.getEndDate();
    if (firstIndexValue != null && scheduleIndex == 0) {
      return createRateComputation(endDate);
    }
    YearMonth referenceStartMonth = YearMonth.from(period.getStartDate().minus(lag));
    YearMonth referenceEndMonth = YearMonth.from(endDate.minus(lag));
    if (indexCalculationMethod.equals(PriceIndexCalculationMethod.INTERPOLATED)) {
      // interpolate between data from two different months
      double weight = 1d - (endDate.getDayOfMonth() - 1d) / endDate.lengthOfMonth();
      return InflationInterpolatedRateComputation.of(index, referenceStartMonth, referenceEndMonth, weight);
    } else if (indexCalculationMethod.equals(PriceIndexCalculationMethod.MONTHLY)) {
      // no interpolation
      return InflationMonthlyRateComputation.of(index, referenceStartMonth, referenceEndMonth);
    } else {
      throw new IllegalArgumentException(
          "PriceIndexCalculationMethod " + indexCalculationMethod.toString() + " is not supported");
    }
  }
 
Example 4
Source File: InflationNodalCurve.java    From Strata with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains an instance from a curve without initial fixing point and month-on-month seasonal adjustment.
 * <p>
 * The total adjustment is computed by accumulation of the monthly adjustment, starting with no adjustment for the 
 * last fixing month.
 * 
 * @param curveWithoutFixing  the curve without the fixing
 * @param valuationDate  the valuation date of the curve
 * @param lastMonth  the last month for which the fixing is known
 * @param lastFixingValue  the value of the last fixing
 * @param seasonalityDefinition  the seasonality definition, which is made of month-on-month adjustment 
 *   and the adjustment type
 * @return the seasonal curve instance
 */
public static InflationNodalCurve of(
    NodalCurve curveWithoutFixing,
    LocalDate valuationDate,
    YearMonth lastMonth,
    double lastFixingValue,
    SeasonalityDefinition seasonalityDefinition) {

  YearMonth valuationMonth = YearMonth.from(valuationDate);
  ArgChecker.isTrue(lastMonth.isBefore(valuationMonth), "Last fixing month must be before valuation date");
  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");
  NodalCurve extendedCurve = curveWithoutFixing.withNode(nbMonth, lastFixingValue, ParameterMetadata.empty());
  double[] seasonalityCompoundedArray = new double[12];
  int lastMonthIndex = lastMonth.getMonth().getValue() - 1;
  seasonalityCompoundedArray[(int) ((nbMonth + 12 + 1) % 12)] =
      seasonalityDefinition.getSeasonalityMonthOnMonth().get(lastMonthIndex % 12);
  for (int i = 1; i < 12; i++) {
    int j = (int) ((nbMonth + 12 + 1 + i) % 12);
    seasonalityCompoundedArray[j] = seasonalityDefinition.getAdjustmentType().applyShift(
        seasonalityCompoundedArray[(j - 1 + 12) % 12],
        seasonalityDefinition.getSeasonalityMonthOnMonth().get((lastMonthIndex + i) % 12));
  }
  return new InflationNodalCurve(
      extendedCurve,
      DoubleArray.ofUnsafe(seasonalityCompoundedArray),
      seasonalityDefinition.getAdjustmentType());
}
 
Example 5
Source File: GsonJsonEngineTest.java    From lastaflute with Apache License 2.0 5 votes vote down vote up
public void test_java8time_toJson_fromJson_yourScalar() throws Exception {
    // ## Arrange ##
    GsonJsonEngine engine = new GsonJsonEngine(builder -> {}, op -> {
        op.yourScalars(Arrays.asList(prepareYearMonthResource()));
    });
    LocalDate date = toLocalDate("2015/05/18");
    LocalDateTime dateTime = toLocalDateTime("2015/05/25 12:34:56.789");
    LocalTime time = toLocalTime("23:15:47.731");
    YearMonth yearMonth = YearMonth.from(toLocalDate("2017/04/01"));
    MockUser mockUser = new MockUser();
    mockUser.id = 2;
    mockUser.name = "land";
    mockUser.birthdate = date;
    mockUser.formalizedDatetime = dateTime;
    mockUser.morningCallTime = time;
    mockUser.schoolBeginningMonth = yearMonth;

    // ## Act ##
    String json = engine.toJson(mockUser);

    // ## Assert ##
    log(json);
    assertContainsAll(json, "2015-05-18", "2015-05-25T12:34:56.789", "23:15:47.731", "\"2017-04\"");

    // ## Act ##
    MockUser fromJson = engine.fromJson(json, MockUser.class);

    // ## Assert ##
    log(fromJson);
    assertEquals("land", fromJson.name);
    assertEquals(toString(fromJson.birthdate, "yyyy-MM-dd"), "2015-05-18");
    assertEquals(toString(fromJson.formalizedDatetime, "yyyy-MM-dd HH:mm:ss.SSS"), "2015-05-25 12:34:56.789");
    assertEquals(toString(fromJson.morningCallTime, "HH:mm:ss.SSS"), "23:15:47.731");
    assertEquals(fromJson.schoolBeginningMonth.toString(), "2017-04");
}
 
Example 6
Source File: RatesCurveGroupDefinition.java    From Strata with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a definition that is bound to a time-series.
 * <p>
 * Curves related to a price index are better described when a starting point is added
 * with the last fixing in the time series. This method finds price index curves, and ensures
 * that they are unique (not used for any other index or discounting). Each price index
 * curve is then bound to the matching time-series with the last fixing month equal to
 * the last element in the time series which is in the past.
 * 
 * @param valuationDate  the valuation date 
 * @param tsMap  the map of index to time series
 * @return the new instance
 */
public RatesCurveGroupDefinition bindTimeSeries(LocalDate valuationDate, Map<Index, LocalDateDoubleTimeSeries> tsMap) {
  ImmutableList.Builder<CurveDefinition> boundCurveDefinitions = ImmutableList.builder();
  for (RatesCurveGroupEntry entry : entries) {
    CurveName name = entry.getCurveName();
    CurveDefinition curveDef = curveDefinitionsByName.get(name);
    Set<Index> indices = entry.getIndices();
    boolean containsPriceIndex = indices.stream().anyMatch(i -> i instanceof PriceIndex);
    if (containsPriceIndex) {
      // check only one curve for Price Index and find time-series last value
      ArgChecker.isTrue(indices.size() == 1, "Price index curve must not relate to another index or discounting: " + name);
      Index index = indices.iterator().next();
      LocalDateDoubleTimeSeries ts = tsMap.get(index);
      ArgChecker.notNull(ts, "Price index curve must have associated time-series: " + index.toString());
      // retrieve last fixing for months before the valuation date
      LocalDateDoubleTimeSeries tsPast = ts.subSeries(ts.getEarliestDate(), valuationDate);
      ArgChecker.isFalse(ts.isEmpty(),
          "Price index curve must have associated time-series with at least one element in the past:" + index.toString());
      ArgChecker.isTrue(curveDef instanceof NodalCurveDefinition,
          "curve definition for inflation curve must be NodalCurveDefinition");
      YearMonth lastFixingMonth = YearMonth.from(tsPast.getLatestDate());
      double lastFixingValue = tsPast.getLatestValue();
      InflationNodalCurveDefinition seasonalCurveDef = new InflationNodalCurveDefinition(
          (NodalCurveDefinition) curveDef, lastFixingMonth, lastFixingValue, seasonalityDefinitions.get(name));
      boundCurveDefinitions.add(seasonalCurveDef);
    } else {
      // no price index
      boundCurveDefinitions.add(curveDef);
    }
  }
  return this.withCurveDefinitions(boundCurveDefinitions.build());
}
 
Example 7
Source File: TCKYearMonth.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_from_TemporalAccessor_invalid_noDerive() {
    YearMonth.from(LocalTime.of(12, 30));
}
 
Example 8
Source File: TCKYearMonth.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    YearMonth.from((TemporalAccessor) null);
}
 
Example 9
Source File: TCKYearMonth.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_from_TemporalAccessor_invalid_noDerive() {
    YearMonth.from(LocalTime.of(12, 30));
}
 
Example 10
Source File: TCKYearMonth.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    YearMonth.from((TemporalAccessor) null);
}
 
Example 11
Source File: TCKYearMonth.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_from_TemporalAccessor_invalid_noDerive() {
    YearMonth.from(LocalTime.of(12, 30));
}
 
Example 12
Source File: TCKYearMonth.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    YearMonth.from((TemporalAccessor) null);
}
 
Example 13
Source File: TCKYearMonth.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    YearMonth.from((TemporalAccessor) null);
}
 
Example 14
Source File: TCKYearMonth.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    YearMonth.from((TemporalAccessor) null);
}
 
Example 15
Source File: TCKYearMonth.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    YearMonth.from((TemporalAccessor) null);
}
 
Example 16
Source File: PostgreSQLYearMonthDateTest.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
@Override
public YearMonth convertToEntityAttribute(java.sql.Date dbData) {
    return YearMonth.from(Instant.ofEpochMilli(dbData.getTime())
            .atZone(ZoneId.systemDefault())
            .toLocalDate());
}
 
Example 17
Source File: TCKYearMonth.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    YearMonth.from((TemporalAccessor) null);
}
 
Example 18
Source File: TCKYearMonth.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_from_TemporalAccessor_invalid_noDerive() {
    YearMonth.from(LocalTime.of(12, 30));
}
 
Example 19
Source File: TCKYearMonth.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_from_TemporalAccessor_invalid_noDerive() {
    YearMonth.from(LocalTime.of(12, 30));
}
 
Example 20
Source File: TCKYearMonth.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_from_TemporalAccessor_invalid_noDerive() {
    YearMonth.from(LocalTime.of(12, 30));
}