下面列出了怎么用java.time.MonthDay的API类实例代码及写法,或者点击链接到github查看源代码。
void doTest_comparisons_MonthDay(MonthDay... localDates) {
for (int i = 0; i < localDates.length; i++) {
MonthDay a = localDates[i];
for (int j = 0; j < localDates.length; j++) {
MonthDay 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);
}
}
}
}
void doTest_comparisons_MonthDay(MonthDay... localDates) {
for (int i = 0; i < localDates.length; i++) {
MonthDay a = localDates[i];
for (int j = 0; j < localDates.length; j++) {
MonthDay 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);
}
}
}
}
void doTest_comparisons_MonthDay(MonthDay... localDates) {
for (int i = 0; i < localDates.length; i++) {
MonthDay a = localDates[i];
for (int j = 0; j < localDates.length; j++) {
MonthDay 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);
}
}
}
}
@Test
public void test_serialization_format() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(baos) ) {
dos.writeByte(13); // java.time.temporal.Ser.MONTH_DAY_TYPE
dos.writeByte(9);
dos.writeByte(16);
}
byte[] bytes = baos.toByteArray();
assertSerializedBySer(MonthDay.of(9, 16), bytes);
}
@DataProvider(name="goodParseData")
Object[][] provider_goodParseData() {
return new Object[][] {
{"--01-01", MonthDay.of(1, 1)},
{"--01-31", MonthDay.of(1, 31)},
{"--02-01", MonthDay.of(2, 1)},
{"--02-29", MonthDay.of(2, 29)},
{"--03-01", MonthDay.of(3, 1)},
{"--03-31", MonthDay.of(3, 31)},
{"--04-01", MonthDay.of(4, 1)},
{"--04-30", MonthDay.of(4, 30)},
{"--05-01", MonthDay.of(5, 1)},
{"--05-31", MonthDay.of(5, 31)},
{"--06-01", MonthDay.of(6, 1)},
{"--06-30", MonthDay.of(6, 30)},
{"--07-01", MonthDay.of(7, 1)},
{"--07-31", MonthDay.of(7, 31)},
{"--08-01", MonthDay.of(8, 1)},
{"--08-31", MonthDay.of(8, 31)},
{"--09-01", MonthDay.of(9, 1)},
{"--09-30", MonthDay.of(9, 30)},
{"--10-01", MonthDay.of(10, 1)},
{"--10-31", MonthDay.of(10, 31)},
{"--11-01", MonthDay.of(11, 1)},
{"--11-30", MonthDay.of(11, 30)},
{"--12-01", MonthDay.of(12, 1)},
{"--12-31", MonthDay.of(12, 31)},
};
}
@Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class)
public void factory_parse_fail(String text, int pos) {
try {
MonthDay.parse(text);
fail(String.format("Parse should have failed for %s at position %d", text, pos));
}
catch (DateTimeParseException ex) {
assertEquals(ex.getParsedString(), text);
assertEquals(ex.getErrorIndex(), pos);
throw ex;
}
}
@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
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(dataProvider = "fields")
public void test_WOWBY_isSupportedBy(TemporalField weekField, TemporalField yearField) {
assertEquals(weekField.isSupportedBy(LocalTime.NOON), false);
assertEquals(weekField.isSupportedBy(MonthDay.of(2, 1)), false);
assertEquals(weekField.isSupportedBy(LocalDate.MIN), true);
assertEquals(weekField.isSupportedBy(OffsetDateTime.MAX), true);
}
@DataProvider(name="atMonthDay")
Object[][] data_atMonthDay() {
return new Object[][] {
{Year.of(2008), MonthDay.of(6, 30), LocalDate.of(2008, 6, 30)},
{Year.of(2008), MonthDay.of(2, 29), LocalDate.of(2008, 2, 29)},
{Year.of(2009), MonthDay.of(2, 29), LocalDate.of(2009, 2, 28)},
};
}
@Test(dataProvider = "fields")
public void test_WOWBY_isSupportedBy(TemporalField weekField, TemporalField yearField) {
assertEquals(weekField.isSupportedBy(LocalTime.NOON), false);
assertEquals(weekField.isSupportedBy(MonthDay.of(2, 1)), false);
assertEquals(weekField.isSupportedBy(LocalDate.MIN), true);
assertEquals(weekField.isSupportedBy(OffsetDateTime.MAX), true);
}
@Override
public EnhancedType<MonthDay> type() {
return EnhancedType.of(MonthDay.class);
}
public MonthDayJacksonDeserializer() {
super(MonthDay.class);
}
@Test
public void test_isValidYear_june() {
MonthDay test = MonthDay.of(6, 30);
assertEquals(test.isValidYear(2007), true);
}
@Test(expectedExceptions=NullPointerException.class)
public void test_factory_CalendricalObject_null() {
MonthDay.from((TemporalAccessor) null);
}
@Test
public void test_adjustDate() {
MonthDay test = MonthDay.of(6, 30);
LocalDate date = LocalDate.of(2007, 1, 1);
assertEquals(test.adjustInto(date), LocalDate.of(2007, 6, 30));
}
@Test
public void test_withMonth() {
assertEquals(MonthDay.of(6, 30).withMonth(1), MonthDay.of(1, 30));
}
@Test
public void test_withMonth_int_noChangeEqual() {
MonthDay test = MonthDay.of(6, 30);
assertEquals(test.withMonth(6), test);
}
@Test
public void factory_intMonth() {
assertEquals(TEST_07_15, MonthDay.of(Month.JULY, 15));
}
@Test
public void test_atYear_int() {
MonthDay test = MonthDay.of(6, 30);
assertEquals(test.atYear(2008), LocalDate.of(2008, 6, 30));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullFormatter() {
MonthDay.parse("ANY", null);
}
@Test(expectedExceptions=DateTimeException.class)
public void test_withMonth_tooLow() {
MonthDay.of(6, 30).withMonth(0);
}
@Test
public void test_with_Month_adjustToValid() {
assertEquals(MonthDay.of(7, 31).with(Month.JUNE), MonthDay.of(6, 30));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullText() {
DateTimeFormatter f = DateTimeFormatter.ofPattern("M d");
MonthDay.parse((String) null, f);
}
@Test
public void test_withMonth_int_noChangeSame() {
MonthDay test = MonthDay.of(6, 30);
assertSame(test.withMonth(6), test);
}
@Test(dataProvider="atMonthDay")
public void test_atMonthDay(Year year, MonthDay monthDay, LocalDate expected) {
assertEquals(year.atMonthDay(monthDay), expected);
}
@Test
public void test_with_Month_adjustToValid() {
assertEquals(MonthDay.of(7, 31).with(Month.JUNE), MonthDay.of(6, 30));
}
@Test(dataProvider="isValidMonthDay")
public void test_isValidMonthDay(Year year, MonthDay monthDay, boolean expected) {
assertEquals(year.isValidMonthDay(monthDay), expected);
}
@Test(expectedExceptions=DateTimeException.class)
public void test_factory_ints_dayTooHigh() {
MonthDay.of(1, 32);
}
@Test(dataProvider="goodParseData")
public void factory_parse_success(String text, MonthDay expected) {
MonthDay monthDay = MonthDay.parse(text);
assertEquals(monthDay, expected);
}