Java Code Examples for java.time.YearMonth

The following examples show how to use java.time.YearMonth. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: cron   Source File: CronExpression.java    License: Apache License 2.0 6 votes vote down vote up
boolean matches(LocalDate dato) {
    for (FieldPart part : parts) {
        if ("L".equals(part.modifier)) {
            YearMonth ym = YearMonth.of(dato.getYear(), dato.getMonth().getValue());
            return dato.getDayOfWeek() == DayOfWeek.of(part.from) && dato.getDayOfMonth() > (ym.lengthOfMonth() - 7);
        } else if ("#".equals(part.incrementModifier)) {
            if (dato.getDayOfWeek() == DayOfWeek.of(part.from)) {
                int num = dato.getDayOfMonth() / 7;
                return part.increment == (dato.getDayOfMonth() % 7 == 0 ? num : num + 1);
            }
            return false;
        } else if (matches(dato.getDayOfWeek().getValue(), part)) {
            return true;
        }
    }
    return false;
}
 
Example 2
Source Project: dragonwell8_jdk   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 6 votes vote down vote up
void doTest_comparisons_YearMonth(YearMonth... localDates) {
    for (int i = 0; i < localDates.length; i++) {
        YearMonth a = localDates[i];
        for (int j = 0; j < localDates.length; j++) {
            YearMonth b = localDates[j];
            if (i < j) {
                assertTrue(a.compareTo(b) < 0, a + " <=> " + b);
                assertEquals(a.isBefore(b), true, a + " <=> " + b);
                assertEquals(a.isAfter(b), false, a + " <=> " + b);
                assertEquals(a.equals(b), false, a + " <=> " + b);
            } else if (i > j) {
                assertTrue(a.compareTo(b) > 0, a + " <=> " + b);
                assertEquals(a.isBefore(b), false, a + " <=> " + b);
                assertEquals(a.isAfter(b), true, a + " <=> " + b);
                assertEquals(a.equals(b), false, a + " <=> " + b);
            } else {
                assertEquals(a.compareTo(b), 0, a + " <=> " + b);
                assertEquals(a.isBefore(b), false, a + " <=> " + b);
                assertEquals(a.isAfter(b), false, a + " <=> " + b);
                assertEquals(a.equals(b), true, a + " <=> " + b);
            }
        }
    }
}
 
Example 3
Source Project: Strata   Source File: EtdFutureSecurityTest.java    License: Apache License 2.0 5 votes vote down vote up
static EtdFutureSecurity sut() {
  return EtdFutureSecurity.builder()
      .info(SecurityInfo.of(SecurityId.of("A", "B"), SecurityPriceInfo.of(Currency.GBP, 100)))
      .contractSpecId(EtdContractSpecId.of("test", "123"))
      .expiry(YearMonth.of(2017, 6))
      .build();
}
 
Example 4
Source Project: LGoodDatePicker   Source File: YearMonthChangeEvent.java    License: MIT License 5 votes vote down vote up
/**
 * Constructor.
 */
public YearMonthChangeEvent(CalendarPanel source,
        YearMonth newYearMonth, YearMonth oldYearMonth) {
    this.source = source;
    this.newYearMonth = newYearMonth;
    this.oldYearMonth = oldYearMonth;
}
 
Example 5
@Test
public void test_padOptional() throws Exception {
    builder.appendValue(MONTH_OF_YEAR).appendLiteral(':')
            .padNext(5).optionalStart().appendValue(DAY_OF_MONTH).optionalEnd()
            .appendLiteral(':').appendValue(YEAR);
    assertEquals(builder.toFormatter().format(LocalDate.of(2013, 2, 1)), "2:    1:2013");
    assertEquals(builder.toFormatter().format(YearMonth.of(2013, 2)), "2:     :2013");
}
 
Example 6
Source Project: Strata   Source File: IborFutureCurveNodeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_metadata_last_fixing() {
  IborFutureCurveNode node =
      IborFutureCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD, LABEL).withDate(CurveNodeDate.LAST_FIXING);
  ImmutableMarketData marketData = ImmutableMarketData.builder(VAL_DATE).addValue(QUOTE_ID, 0.0d).build();
  IborFutureTrade trade = node.trade(1d, marketData, REF_DATA);
  LocalDate fixingDate = trade.getProduct().getFixingDate();
  DatedParameterMetadata metadata = node.metadata(VAL_DATE, REF_DATA);
  assertThat(metadata.getDate()).isEqualTo(fixingDate);
  LocalDate referenceDate = TEMPLATE.calculateReferenceDateFromTradeDate(VAL_DATE, REF_DATA);
  assertThat(((YearMonthDateParameterMetadata) metadata).getYearMonth()).isEqualTo(YearMonth.from(referenceDate));
}
 
Example 7
@Test
public void test_isValidDay_int_june() {
    YearMonth test = YearMonth.of(2007, 6);
    assertEquals(test.isValidDay(1), true);
    assertEquals(test.isValidDay(30), true);

    assertEquals(test.isValidDay(-1), false);
    assertEquals(test.isValidDay(0), false);
    assertEquals(test.isValidDay(31), false);
    assertEquals(test.isValidDay(32), false);
}
 
Example 8
@Test
public void test_serialization_format() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream dos = new DataOutputStream(baos) ) {
        dos.writeByte(12);       // java.time.temporal.Ser.YEAR_MONTH_TYPE
        dos.writeInt(2012);
        dos.writeByte(9);
    }
    byte[] bytes = baos.toByteArray();
    assertSerializedBySer(YearMonth.of(2012, 9), bytes);
}
 
Example 9
@Test
public void test_parse_errorMessage() throws Exception {
    assertGoodErrorDate(DayOfWeek::from, "DayOfWeek");
    assertGoodErrorDate(Month::from, "Month");
    assertGoodErrorDate(YearMonth::from, "YearMonth");
    assertGoodErrorDate(MonthDay::from, "MonthDay");
    assertGoodErrorDate(LocalDate::from, "LocalDate");
    assertGoodErrorDate(LocalTime::from, "LocalTime");
    assertGoodErrorDate(LocalDateTime::from, "LocalDateTime");
    assertGoodErrorDate(OffsetTime::from, "OffsetTime");
    assertGoodErrorDate(OffsetDateTime::from, "OffsetDateTime");
    assertGoodErrorDate(ZonedDateTime::from, "ZonedDateTime");
    assertGoodErrorDate(Instant::from, "Instant");
    assertGoodErrorDate(ZoneOffset::from, "ZoneOffset");
    assertGoodErrorDate(ZoneId::from, "ZoneId");
    assertGoodErrorDate(ThaiBuddhistChronology.INSTANCE::date, "");

    assertGoodErrorTime(DayOfWeek::from, "DayOfWeek");
    assertGoodErrorTime(Month::from, "Month");
    assertGoodErrorTime(Year::from, "Year");
    assertGoodErrorTime(YearMonth::from, "YearMonth");
    assertGoodErrorTime(MonthDay::from, "MonthDay");
    assertGoodErrorTime(LocalDate::from, "LocalDate");
    assertGoodErrorTime(LocalTime::from, "LocalTime");
    assertGoodErrorTime(LocalDateTime::from, "LocalDateTime");
    assertGoodErrorTime(OffsetTime::from, "OffsetTime");
    assertGoodErrorTime(OffsetDateTime::from, "OffsetDateTime");
    assertGoodErrorTime(ZonedDateTime::from, "ZonedDateTime");
    assertGoodErrorTime(Instant::from, "Instant");
    assertGoodErrorTime(ZoneOffset::from, "ZoneOffset");
    assertGoodErrorTime(ZoneId::from, "ZoneId");
    assertGoodErrorTime(ThaiBuddhistChronology.INSTANCE::date, "");
}
 
Example 10
@Test
public void test_isValidDay_int_febLeap() {
    YearMonth test = YearMonth.of(2008, 2);
    assertEquals(test.isValidDay(1), true);
    assertEquals(test.isValidDay(29), true);

    assertEquals(test.isValidDay(-1), false);
    assertEquals(test.isValidDay(0), false);
    assertEquals(test.isValidDay(30), false);
    assertEquals(test.isValidDay(32), false);
}
 
Example 11
Source Project: dragonwell8_jdk   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_ZoneId() {
    ZoneId zone = ZoneId.of("UTC+01:02:03");
    YearMonth expected = YearMonth.now(Clock.system(zone));
    YearMonth test = YearMonth.now(zone);
    for (int i = 0; i < 100; i++) {
        if (expected.equals(test)) {
            return;
        }
        expected = YearMonth.now(Clock.system(zone));
        test = YearMonth.now(zone);
    }
    assertEquals(test, expected);
}
 
Example 12
Source Project: hottub   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="plus_TemporalAmount")
public void test_plus_TemporalAmount(YearMonth base, TemporalAmount temporalAmount, YearMonth expectedYearMonth, Class<?> expectedEx) {
    if (expectedEx == null) {
        assertEquals(base.plus(temporalAmount), expectedYearMonth);
    } else {
        try {
            YearMonth result = base.plus(temporalAmount);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 13
Source Project: hottub   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_equals() {
    YearMonth a = YearMonth.of(2008, 6);
    YearMonth b = YearMonth.of(2008, 6);
    YearMonth c = YearMonth.of(2007, 6);
    YearMonth d = YearMonth.of(2008, 5);

    assertEquals(a.equals(a), true);
    assertEquals(a.equals(b), true);
    assertEquals(a.equals(c), false);
    assertEquals(a.equals(d), false);

    assertEquals(b.equals(a), true);
    assertEquals(b.equals(b), true);
    assertEquals(b.equals(c), false);
    assertEquals(b.equals(d), false);

    assertEquals(c.equals(a), false);
    assertEquals(c.equals(b), false);
    assertEquals(c.equals(c), true);
    assertEquals(c.equals(d), false);

    assertEquals(d.equals(a), false);
    assertEquals(d.equals(b), false);
    assertEquals(d.equals(c), false);
    assertEquals(d.equals(d), true);
}
 
Example 14
Source Project: dragonwell8_jdk   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="goodParseData")
Object[][] provider_goodParseData() {
    return new Object[][] {
            {"0000-01", YearMonth.of(0, 1)},
            {"0000-12", YearMonth.of(0, 12)},
            {"9999-12", YearMonth.of(9999, 12)},
            {"2000-01", YearMonth.of(2000, 1)},
            {"2000-02", YearMonth.of(2000, 2)},
            {"2000-03", YearMonth.of(2000, 3)},
            {"2000-04", YearMonth.of(2000, 4)},
            {"2000-05", YearMonth.of(2000, 5)},
            {"2000-06", YearMonth.of(2000, 6)},
            {"2000-07", YearMonth.of(2000, 7)},
            {"2000-08", YearMonth.of(2000, 8)},
            {"2000-09", YearMonth.of(2000, 9)},
            {"2000-10", YearMonth.of(2000, 10)},
            {"2000-11", YearMonth.of(2000, 11)},
            {"2000-12", YearMonth.of(2000, 12)},

            {"+12345678-03", YearMonth.of(12345678, 3)},
            {"+123456-03", YearMonth.of(123456, 3)},
            {"0000-03", YearMonth.of(0, 3)},
            {"-1234-03", YearMonth.of(-1234, 3)},
            {"-12345678-03", YearMonth.of(-12345678, 3)},

            {"+" + Year.MAX_VALUE + "-03", YearMonth.of(Year.MAX_VALUE, 3)},
            {Year.MIN_VALUE + "-03", YearMonth.of(Year.MIN_VALUE, 3)},
    };
}
 
Example 15
@Test
public void test_padOptional() throws Exception {
    builder.appendValue(MONTH_OF_YEAR).appendLiteral(':')
            .padNext(5).optionalStart().appendValue(DAY_OF_MONTH).optionalEnd()
            .appendLiteral(':').appendValue(YEAR);
    assertEquals(builder.toFormatter().format(LocalDate.of(2013, 2, 1)), "2:    1:2013");
    assertEquals(builder.toFormatter().format(YearMonth.of(2013, 2)), "2:     :2013");
}
 
Example 16
@Test
public void isExpired3()
{
  final YearMonth expirationDateLastMonth = YearMonth.now()
    .minus(1, ChronoUnit.MONTHS);
  check(expirationDateLastMonth, true);
}
 
Example 17
Source Project: TencentKona-8   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="minus_long_TemporalUnit")
public void test_minus_long_TemporalUnit(YearMonth base, long amount, TemporalUnit unit, YearMonth expectedYearMonth, Class<?> expectedEx) {
    if (expectedEx == null) {
        assertEquals(base.minus(amount, unit), expectedYearMonth);
    } else {
        try {
            YearMonth result = base.minus(amount, unit);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 18
Source Project: jdk8u60   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_isValidDay_int_febNonLeap() {
    YearMonth test = YearMonth.of(2007, 2);
    assertEquals(test.isValidDay(1), true);
    assertEquals(test.isValidDay(28), true);

    assertEquals(test.isValidDay(-1), false);
    assertEquals(test.isValidDay(0), false);
    assertEquals(test.isValidDay(29), false);
    assertEquals(test.isValidDay(32), false);
}
 
Example 19
Source Project: jdk8u-jdk   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_isValidDay_int_febLeap() {
    YearMonth test = YearMonth.of(2008, 2);
    assertEquals(test.isValidDay(1), true);
    assertEquals(test.isValidDay(29), true);

    assertEquals(test.isValidDay(-1), false);
    assertEquals(test.isValidDay(0), false);
    assertEquals(test.isValidDay(30), false);
    assertEquals(test.isValidDay(32), false);
}
 
Example 20
@Test(dataProvider="sampleDates")
public void test_get(int y, int m) {
    YearMonth a = YearMonth.of(y, m);
    assertEquals(a.getYear(), y);
    assertEquals(a.getMonth(), Month.of(m));
    assertEquals(a.getMonthValue(), m);
}
 
Example 21
@Test
public void testSerializationAsTimestamp01() throws Exception
{
    YearMonth yearMonth = YearMonth.of(1986, Month.JANUARY);
    String value = MAPPER.writer()
    		.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
    		.writeValueAsString(yearMonth);

    assertNotNull("The value should not be null.", value);
    assertEquals("The value is not correct.", "[1986,1]", value);
}
 
Example 22
Source Project: jdk8u_jdk   Source File: TCKMonthDay.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_hashCode_unique() {
    int leapYear = 2008;
    Set<Integer> uniques = new HashSet<Integer>(366);
    for (int i = 1; i <= 12; i++) {
        for (int j = 1; j <= 31; j++) {
            if (YearMonth.of(leapYear, i).isValidDay(j)) {
                assertTrue(uniques.add(MonthDay.of(i, j).hashCode()));
            }
        }
    }
}
 
Example 23
Source Project: jdk8u_jdk   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="minus_long_TemporalUnit")
public void test_minus_long_TemporalUnit(YearMonth base, long amount, TemporalUnit unit, YearMonth expectedYearMonth, Class<?> expectedEx) {
    if (expectedEx == null) {
        assertEquals(base.minus(amount, unit), expectedYearMonth);
    } else {
        try {
            YearMonth result = base.minus(amount, unit);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 24
@Test
public void test_equals() {
    YearMonth a = YearMonth.of(2008, 6);
    YearMonth b = YearMonth.of(2008, 6);
    YearMonth c = YearMonth.of(2007, 6);
    YearMonth d = YearMonth.of(2008, 5);

    assertEquals(a.equals(a), true);
    assertEquals(a.equals(b), true);
    assertEquals(a.equals(c), false);
    assertEquals(a.equals(d), false);

    assertEquals(b.equals(a), true);
    assertEquals(b.equals(b), true);
    assertEquals(b.equals(c), false);
    assertEquals(b.equals(d), false);

    assertEquals(c.equals(a), false);
    assertEquals(c.equals(b), false);
    assertEquals(c.equals(c), true);
    assertEquals(c.equals(d), false);

    assertEquals(d.equals(a), false);
    assertEquals(d.equals(b), false);
    assertEquals(d.equals(c), false);
    assertEquals(d.equals(d), true);
}
 
Example 25
Source Project: Strata   Source File: SimplePriceIndexValues.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 26
Source Project: openjdk-jdk8u   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 4 votes vote down vote up
@BeforeMethod
public void setUp() {
    TEST_2008_06 = YearMonth.of(2008, 6);
}
 
Example 27
Source Project: hottub   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_with_Month_null() {
    YearMonth test = YearMonth.of(2008, 6);
    test.with((Month) null);
}
 
Example 28
Source Project: jdk8u-jdk   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth() {
    YearMonth test = YearMonth.of(2008, 6);
    assertEquals(test.withMonth(1), YearMonth.of(2008, 1));
}
 
Example 29
Source Project: LGoodDatePicker   Source File: YearMonthChangeEvent.java    License: MIT License 4 votes vote down vote up
/**
 * getOldYearMonth, Returns the old YearMonth. This will never return null.
 */
public YearMonth getOldYearMonth() {
    return oldYearMonth;
}
 
Example 30
Source Project: openjdk-jdk8u-backup   Source File: TCKYear.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_until_convertedType() {
    Year start = Year.of(2010);
    YearMonth end = start.plusYears(2).atMonth(Month.APRIL);
    assertEquals(start.until(end, YEARS), 2);
}