下面列出了怎么用java.time.Year的API类实例代码及写法,或者点击链接到github查看源代码。
@DataProvider(name="plus_long_TemporalUnit")
Object[][] data_plus_long_TemporalUnit() {
return new Object[][] {
{Year.of(1), 1, ChronoUnit.YEARS, Year.of(2), null},
{Year.of(1), -12, ChronoUnit.YEARS, Year.of(-11), null},
{Year.of(1), 0, ChronoUnit.YEARS, Year.of(1), null},
{Year.of(999999999), 0, ChronoUnit.YEARS, Year.of(999999999), null},
{Year.of(-999999999), 0, ChronoUnit.YEARS, Year.of(-999999999), null},
{Year.of(0), -999999999, ChronoUnit.YEARS, Year.of(-999999999), null},
{Year.of(0), 999999999, ChronoUnit.YEARS, Year.of(999999999), null},
{Year.of(-1), 1, ChronoUnit.ERAS, Year.of(2), null},
{Year.of(5), 1, ChronoUnit.CENTURIES, Year.of(105), null},
{Year.of(5), 1, ChronoUnit.DECADES, Year.of(15), null},
{Year.of(999999999), 1, ChronoUnit.YEARS, null, DateTimeException.class},
{Year.of(-999999999), -1, ChronoUnit.YEARS, null, DateTimeException.class},
{Year.of(1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
{Year.of(1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
{Year.of(1), 0, ChronoUnit.MONTHS, null, DateTimeException.class},
};
}
@DataProvider(name="minus_long_TemporalUnit")
Object[][] data_minus_long_TemporalUnit() {
return new Object[][] {
{Year.of(1), 1, ChronoUnit.YEARS, Year.of(0), null},
{Year.of(1), -12, ChronoUnit.YEARS, Year.of(13), null},
{Year.of(1), 0, ChronoUnit.YEARS, Year.of(1), null},
{Year.of(999999999), 0, ChronoUnit.YEARS, Year.of(999999999), null},
{Year.of(-999999999), 0, ChronoUnit.YEARS, Year.of(-999999999), null},
{Year.of(0), -999999999, ChronoUnit.YEARS, Year.of(999999999), null},
{Year.of(0), 999999999, ChronoUnit.YEARS, Year.of(-999999999), null},
{Year.of(999999999), 1, ChronoUnit.ERAS, Year.of(-999999999 + 1), null},
{Year.of(105), 1, ChronoUnit.CENTURIES, Year.of(5), null},
{Year.of(15), 1, ChronoUnit.DECADES, Year.of(5), null},
{Year.of(-999999999), 1, ChronoUnit.YEARS, null, DateTimeException.class},
{Year.of(1), -999999999, ChronoUnit.YEARS, null, DateTimeException.class},
{Year.of(1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
{Year.of(1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
{Year.of(1), 0, ChronoUnit.MONTHS, null, DateTimeException.class},
};
}
@Test
public void test_atDay_notLeapYear() {
Year test = Year.of(2007);
LocalDate expected = LocalDate.of(2007, 1, 1);
for (int i = 1; i <= 365; i++) {
assertEquals(test.atDay(i), expected);
expected = expected.plusDays(1);
}
}
@Test(dataProvider="prolepticYear")
public void test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) {
assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear);
assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap());
MinguoDate minguo = MinguoDate.now();
minguo = minguo.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2);
if (isLeapYear) {
assertEquals(minguo.lengthOfMonth(), 29);
} else {
assertEquals(minguo.lengthOfMonth(), 28);
}
}
@DataProvider(name="isValidMonthDay")
Object[][] data_isValidMonthDay() {
return new Object[][] {
{Year.of(2007), MonthDay.of(6, 30), true},
{Year.of(2008), MonthDay.of(2, 28), true},
{Year.of(2008), MonthDay.of(2, 29), true},
{Year.of(2009), MonthDay.of(2, 28), true},
{Year.of(2009), MonthDay.of(2, 29), false},
{Year.of(2009), null, false},
};
}
@Test
public void test_toString() {
for (int i = -4; i <= 2104; i++) {
Year a = Year.of(i);
assertEquals(a.toString(), "" + i);
}
}
@Test(dataProvider="prolepticYear")
public void test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) {
assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear);
assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap());
MinguoDate minguo = MinguoDate.now();
minguo = minguo.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2);
if (isLeapYear) {
assertEquals(minguo.lengthOfMonth(), 29);
} else {
assertEquals(minguo.lengthOfMonth(), 28);
}
}
@Test
public void test_factory_int_singleton() {
for (int i = -4; i <= 2104; i++) {
Year test = Year.of(i);
assertEquals(test.getValue(), i);
assertEquals(Year.of(i), test);
}
}
@Test(dataProvider="prolepticYear")
public void test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) {
assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear);
assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap());
MinguoDate minguo = MinguoDate.now();
minguo = minguo.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2);
if (isLeapYear) {
assertEquals(minguo.lengthOfMonth(), 29);
} else {
assertEquals(minguo.lengthOfMonth(), 28);
}
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_tooLow() {
long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
int year = Year.MIN_VALUE - 1;
long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
}
@Test
public void test_compareTo_max() {
OffsetDateTime a = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MONE);
OffsetDateTime b = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MTWO); // a is before b due to offset
assertEquals(a.compareTo(b) < 0, true);
assertEquals(b.compareTo(a) > 0, true);
assertEquals(a.compareTo(a) == 0, true);
assertEquals(b.compareTo(b) == 0, true);
}
@Test
public void test_toString() {
for (int i = -4; i <= 2104; i++) {
Year a = Year.of(i);
assertEquals(a.toString(), "" + i);
}
}
@Test
public void test_plusYears() {
assertEquals(Year.of(2007).plusYears(-1), Year.of(2006));
assertEquals(Year.of(2007).plusYears(0), Year.of(2007));
assertEquals(Year.of(2007).plusYears(1), Year.of(2008));
assertEquals(Year.of(2007).plusYears(2), Year.of(2009));
assertEquals(Year.of(Year.MAX_VALUE - 1).plusYears(1), Year.of(Year.MAX_VALUE));
assertEquals(Year.of(Year.MAX_VALUE).plusYears(0), Year.of(Year.MAX_VALUE));
assertEquals(Year.of(Year.MIN_VALUE + 1).plusYears(-1), Year.of(Year.MIN_VALUE));
assertEquals(Year.of(Year.MIN_VALUE).plusYears(0), Year.of(Year.MIN_VALUE));
}
@Test
public void test_length() {
assertEquals(Year.of(1999).length(), 365);
assertEquals(Year.of(2000).length(), 366);
assertEquals(Year.of(2001).length(), 365);
assertEquals(Year.of(2007).length(), 365);
assertEquals(Year.of(2008).length(), 366);
assertEquals(Year.of(2009).length(), 365);
assertEquals(Year.of(2010).length(), 365);
assertEquals(Year.of(2011).length(), 365);
assertEquals(Year.of(2012).length(), 366);
assertEquals(Year.of(2095).length(), 365);
assertEquals(Year.of(2096).length(), 366);
assertEquals(Year.of(2097).length(), 365);
assertEquals(Year.of(2098).length(), 365);
assertEquals(Year.of(2099).length(), 365);
assertEquals(Year.of(2100).length(), 365);
assertEquals(Year.of(2101).length(), 365);
assertEquals(Year.of(2102).length(), 365);
assertEquals(Year.of(2103).length(), 365);
assertEquals(Year.of(2104).length(), 366);
assertEquals(Year.of(2105).length(), 365);
assertEquals(Year.of(-500).length(), 365);
assertEquals(Year.of(-400).length(), 366);
assertEquals(Year.of(-300).length(), 365);
assertEquals(Year.of(-200).length(), 365);
assertEquals(Year.of(-100).length(), 365);
assertEquals(Year.of(0).length(), 366);
assertEquals(Year.of(100).length(), 365);
assertEquals(Year.of(200).length(), 365);
assertEquals(Year.of(300).length(), 365);
assertEquals(Year.of(400).length(), 366);
assertEquals(Year.of(500).length(), 365);
}
@DataProvider(name="isValidMonthDay")
Object[][] data_isValidMonthDay() {
return new Object[][] {
{Year.of(2007), MonthDay.of(6, 30), true},
{Year.of(2008), MonthDay.of(2, 28), true},
{Year.of(2008), MonthDay.of(2, 29), true},
{Year.of(2009), MonthDay.of(2, 28), true},
{Year.of(2009), MonthDay.of(2, 29), false},
{Year.of(2009), null, false},
};
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_tooLow() {
long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
int year = Year.MIN_VALUE - 1;
long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
}
@Test
public void test_compareTo_min() {
OffsetDateTime a = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PTWO);
OffsetDateTime b = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PONE); // a is before b due to offset
assertEquals(a.compareTo(b) < 0, true);
assertEquals(b.compareTo(a) > 0, true);
assertEquals(a.compareTo(a) == 0, true);
assertEquals(b.compareTo(b) == 0, true);
}
@Test
public void test_with_adjuster_Year() {
LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0);
ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100);
ZonedDateTime test = base.with(Year.of(2007));
assertEquals(test, ZonedDateTime.of(ldt.withYear(2007), ZONE_0100));
}
@Test
public void test_compareTo_max() {
OffsetDateTime a = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MONE);
OffsetDateTime b = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MTWO); // a is before b due to offset
assertEquals(a.compareTo(b) < 0, true);
assertEquals(b.compareTo(a) > 0, true);
assertEquals(a.compareTo(a) == 0, true);
assertEquals(b.compareTo(b) == 0, true);
}
@Test
public void test_minusYears() {
assertEquals(Year.of(2007).minusYears(-1), Year.of(2008));
assertEquals(Year.of(2007).minusYears(0), Year.of(2007));
assertEquals(Year.of(2007).minusYears(1), Year.of(2006));
assertEquals(Year.of(2007).minusYears(2), Year.of(2005));
assertEquals(Year.of(Year.MAX_VALUE - 1).minusYears(-1), Year.of(Year.MAX_VALUE));
assertEquals(Year.of(Year.MAX_VALUE).minusYears(0), Year.of(Year.MAX_VALUE));
assertEquals(Year.of(Year.MIN_VALUE + 1).minusYears(1), Year.of(Year.MIN_VALUE));
assertEquals(Year.of(Year.MIN_VALUE).minusYears(0), Year.of(Year.MIN_VALUE));
}
@Test(expectedExceptions=DateTimeException.class)
public void test_minusYears_long_invalidTooSmall() {
LocalDate.of(Year.MIN_VALUE, 1, 1).minusYears(1);
}
@Test(expectedExceptions=DateTimeException.class)
public void test_plusYears_long_invalidTooSmall() {
YearMonth test = YearMonth.of(Year.MIN_VALUE, 6);
test.plusYears(-1);
}
@Test(expectedExceptions=DateTimeException.class)
public void test_withYear_int_invalid() {
TEST_2007_07_15.withYear(Year.MIN_VALUE - 1);
}
private int currentYear() {
return Year.now()
.getValue();
}
@Test(expected=NullPointerException.class)
public void test_compareTo_nullYear() {
Year doy = null;
Year test = Year.of(1);
test.compareTo(doy);
}
@Test(expectedExceptions={ArithmeticException.class})
public void test_plusWeeks_invalidMaxMinusMin() {
LocalDate.of(Year.MAX_VALUE, 12, 25).plusWeeks(Long.MIN_VALUE);
}
@Test
public void test_plusWeeks_minimum() {
LocalDate t = LocalDate.of(Year.MIN_VALUE, 1, 8).plusWeeks(-1);
LocalDate expected = LocalDate.of(Year.MIN_VALUE, 1, 1);
assertEquals(t, expected);
}
@Test(expectedExceptions=DateTimeException.class)
public void test_plusMonths_long_invalidTooLargeMaxAddMax() {
LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1);
test.plusMonths(Long.MAX_VALUE);
}
@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);
}
@Test(expectedExceptions=DateTimeException.class)
public void test_plusMonths_int_invalidTooSmall() {
createDateMidnight(Year.MIN_VALUE, 1, 1).plusMonths(-1);
}