下面列出了com.fasterxml.jackson.core.json.PackageVersion#java.time.YearMonth 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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);
}
}
}
}
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();
}
/**
* Constructor.
*/
public YearMonthChangeEvent(CalendarPanel source,
YearMonth newYearMonth, YearMonth oldYearMonth) {
this.source = source;
this.newYearMonth = newYearMonth;
this.oldYearMonth = oldYearMonth;
}
@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");
}
@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));
}
@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);
}
@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);
}
@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, "");
}
@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);
}
@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);
}
@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));
}
}
}
@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);
}
@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)},
};
}
@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");
}
@Test
public void isExpired3()
{
final YearMonth expirationDateLastMonth = YearMonth.now()
.minus(1, ChronoUnit.MONTHS);
check(expirationDateLastMonth, true);
}
@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));
}
}
}
@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);
}
@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);
}
@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);
}
@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);
}
@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()));
}
}
}
}
@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));
}
}
}
@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);
}
@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);
}
@BeforeMethod
public void setUp() {
TEST_2008_06 = YearMonth.of(2008, 6);
}
@Test(expectedExceptions=NullPointerException.class)
public void test_with_Month_null() {
YearMonth test = YearMonth.of(2008, 6);
test.with((Month) null);
}
@Test
public void test_withMonth() {
YearMonth test = YearMonth.of(2008, 6);
assertEquals(test.withMonth(1), YearMonth.of(2008, 1));
}
/**
* getOldYearMonth, Returns the old YearMonth. This will never return null.
*/
public YearMonth getOldYearMonth() {
return oldYearMonth;
}
@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);
}