下面列出了java.time.Period#of ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Period until(ChronoLocalDate endDate) {
// TODO: untested
CopticDate end = getChronology().date(endDate);
long totalMonths = (end.prolepticYear - this.prolepticYear) * 13 + (end.month - this.month); // safe
int days = end.day - this.day;
if (totalMonths > 0 && days < 0) {
totalMonths--;
CopticDate calcDate = this.plusMonths(totalMonths);
days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe
} else if (totalMonths < 0 && days > 0) {
totalMonths++;
days -= end.lengthOfMonth();
}
long years = totalMonths / 13; // safe
int months = (int) (totalMonths % 13); // safe
return Period.of(Math.toIntExact(years), months, days);
}
XmlAdaptersTest() throws JAXBException {
jaxbContext = JAXBContext.newInstance(Bean.class);
final Bean expectedBean = new Bean();
expectedBean.duration = Duration.ofDays(2).plusHours(3).plusMinutes(4);
expectedBean.instant = LocalDateTime.of(2007, 12, 3, 10, 15, 30)
.toInstant(ZoneOffset.UTC);
expectedBean.localDateTime = LocalDateTime.of(2007, 12, 3, 10, 15, 30);
expectedBean.localDate = LocalDate.of(2014, 12, 31);
expectedBean.localTime = LocalTime.of(10, 15, 30);
expectedBean.monthDay = MonthDay.of(Month.DECEMBER, 3);
expectedBean.offsetDateTime = OffsetDateTime
.of(2007, 12, 3, 10, 15, 30, 0, ZoneOffset.ofHours(1));
expectedBean.offsetTime = OffsetTime
.of(10, 15, 30, 0, ZoneOffset.ofHours(1));
expectedBean.period = Period.of(1, 2, 25);
expectedBean.year = Year.of(-2014);
expectedBean.yearMonth = YearMonth.of(2014, 12);
expectedBean.zoneId = ZoneId.of("America/New_York");
expectedBean.zoneOffset = ZoneOffset.ofHoursMinutes(-12, 0);
expectedBean.zonedDateTime = ZonedDateTime
.of(2007, 12, 3, 10, 15, 30, 0, ZoneId.of("Europe/Paris"));
this.expectedBean = expectedBean;
this.expectedMarshalledBean = getClass().getResource("expectedBean.xml");
}
@Test(dataProvider="calendars", expectedExceptions=DateTimeException.class)
public void test_plus_wrongChrono(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoPeriod isoPeriod = Period.of(2, 3, 4);
ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4);
// one of these two will fail
period.plus(isoPeriod);
period.plus(thaiPeriod);
}
@DataProvider(name="plusInvalidUnit")
Object[][] data_plusInvalidUnit() {
return new Object[][] {
{Period.of(0, 1, 0)},
{Period.of(0, 0, 1)},
{Period.of(0, 1, 1)},
{Period.of(1, 1, 1)},
{Duration.ofDays(1)},
{Duration.ofHours(1)},
{Duration.ofMinutes(1)},
{Duration.ofSeconds(1)},
};
}
@DataProvider(name="toStringAndParse")
Object[][] data_toString() {
return new Object[][] {
{Period.ZERO, "P0D"},
{Period.ofDays(0), "P0D"},
{Period.ofYears(1), "P1Y"},
{Period.ofMonths(1), "P1M"},
{Period.ofDays(1), "P1D"},
{Period.of(1, 2, 0), "P1Y2M"},
{Period.of(0, 2, 3), "P2M3D"},
{Period.of(1, 2, 3), "P1Y2M3D"},
};
}
@DataProvider(name="plusInvalidUnit")
Object[][] data_plusInvalidUnit() {
return new Object[][] {
{Period.of(0, 1, 0)},
{Period.of(0, 0, 1)},
{Period.of(0, 1, 1)},
{Period.of(1, 1, 1)},
{Duration.ofDays(1)},
{Duration.ofHours(1)},
{Duration.ofMinutes(1)},
{Duration.ofSeconds(1)},
};
}
@Test
public void testDeserialization01() throws Exception
{
Period period = Period.of(1, 6, 15);
Period value = MAPPER.readValue('"' + period.toString() + '"', Period.class);
assertEquals("The value is not correct.", period, value);
}
@DataProvider(name="plusInvalidUnit")
Object[][] data_plusInvalidUnit() {
return new Object[][] {
{Period.of(0, 1, 0)},
{Period.of(0, 0, 1)},
{Period.of(0, 1, 1)},
{Period.of(1, 1, 1)},
{Duration.ofDays(1)},
{Duration.ofHours(1)},
{Duration.ofMinutes(1)},
{Duration.ofSeconds(1)},
};
}
public void test_equals_otherClass() {
Period test = Period.of(1, 2, 3);
assertEquals(test.equals(""), false);
}
private static Period pymd(int y, int m, int d) {
return Period.of(y, m, d);
}
@Test
public void factory_from_TemporalAmount_Period() {
TemporalAmount amount = Period.of(1, 2, 3);
assertPeriod(Period.from(amount), 1, 2, 3);
}
public void test_equals_self() {
Period test = Period.of(1, 2, 3);
assertEquals(test.equals(test), true);
}
@Test(dataProvider="BadTemporalUnit", expectedExceptions=DateTimeException.class)
public void test_bad_getUnit(TemporalUnit unit) {
Period period = Period.of(2, 2, 2);
period.get(unit);
}
@DataProvider(name="parseSuccess")
Object[][] data_factory_parseSuccess() {
return new Object[][] {
{"P1Y", Period.ofYears(1)},
{"P12Y", Period.ofYears(12)},
{"P987654321Y", Period.ofYears(987654321)},
{"P+1Y", Period.ofYears(1)},
{"P+12Y", Period.ofYears(12)},
{"P+987654321Y", Period.ofYears(987654321)},
{"P+0Y", Period.ofYears(0)},
{"P0Y", Period.ofYears(0)},
{"P-0Y", Period.ofYears(0)},
{"P-25Y", Period.ofYears(-25)},
{"P-987654321Y", Period.ofYears(-987654321)},
{"P" + Integer.MAX_VALUE + "Y", Period.ofYears(Integer.MAX_VALUE)},
{"P" + Integer.MIN_VALUE + "Y", Period.ofYears(Integer.MIN_VALUE)},
{"P1M", Period.ofMonths(1)},
{"P12M", Period.ofMonths(12)},
{"P987654321M", Period.ofMonths(987654321)},
{"P+1M", Period.ofMonths(1)},
{"P+12M", Period.ofMonths(12)},
{"P+987654321M", Period.ofMonths(987654321)},
{"P+0M", Period.ofMonths(0)},
{"P0M", Period.ofMonths(0)},
{"P-0M", Period.ofMonths(0)},
{"P-25M", Period.ofMonths(-25)},
{"P-987654321M", Period.ofMonths(-987654321)},
{"P" + Integer.MAX_VALUE + "M", Period.ofMonths(Integer.MAX_VALUE)},
{"P" + Integer.MIN_VALUE + "M", Period.ofMonths(Integer.MIN_VALUE)},
{"P1W", Period.ofDays(1 * 7)},
{"P12W", Period.ofDays(12 * 7)},
{"P7654321W", Period.ofDays(7654321 * 7)},
{"P+1W", Period.ofDays(1 * 7)},
{"P+12W", Period.ofDays(12 * 7)},
{"P+7654321W", Period.ofDays(7654321 * 7)},
{"P+0W", Period.ofDays(0)},
{"P0W", Period.ofDays(0)},
{"P-0W", Period.ofDays(0)},
{"P-25W", Period.ofDays(-25 * 7)},
{"P-7654321W", Period.ofDays(-7654321 * 7)},
{"P1D", Period.ofDays(1)},
{"P12D", Period.ofDays(12)},
{"P987654321D", Period.ofDays(987654321)},
{"P+1D", Period.ofDays(1)},
{"P+12D", Period.ofDays(12)},
{"P+987654321D", Period.ofDays(987654321)},
{"P+0D", Period.ofDays(0)},
{"P0D", Period.ofDays(0)},
{"P-0D", Period.ofDays(0)},
{"P-25D", Period.ofDays(-25)},
{"P-987654321D", Period.ofDays(-987654321)},
{"P" + Integer.MAX_VALUE + "D", Period.ofDays(Integer.MAX_VALUE)},
{"P" + Integer.MIN_VALUE + "D", Period.ofDays(Integer.MIN_VALUE)},
{"P0Y0M0D", Period.of(0, 0, 0)},
{"P2Y0M0D", Period.of(2, 0, 0)},
{"P0Y3M0D", Period.of(0, 3, 0)},
{"P0Y0M4D", Period.of(0, 0, 4)},
{"P2Y3M25D", Period.of(2, 3, 25)},
{"P-2Y3M25D", Period.of(-2, 3, 25)},
{"P2Y-3M25D", Period.of(2, -3, 25)},
{"P2Y3M-25D", Period.of(2, 3, -25)},
{"P-2Y-3M-25D", Period.of(-2, -3, -25)},
{"P0Y0M0W0D", Period.of(0, 0, 0)},
{"P2Y3M4W25D", Period.of(2, 3, 4 * 7 + 25)},
{"P-2Y-3M-4W-25D", Period.of(-2, -3, -4 * 7 - 25)},
};
}
@Override
public Period readResolve() {
return Period.of(years, months, days);
}
public void test_equals_otherClass() {
Period test = Period.of(1, 2, 3);
assertEquals(test.equals(""), false);
}
/**
* Obtains a period for this chronology based on years, months and days.
* <p>
* This returns a period tied to the ISO chronology using the specified
* years, months and days. See {@link Period} for further details.
*
* @param years the number of years, may be negative
* @param months the number of years, may be negative
* @param days the number of years, may be negative
* @return the period in terms of this chronology, not null
* @return the ISO period, not null
*/
@Override // override with covariant return type
public Period period(int years, int months, int days) {
return Period.of(years, months, days);
}
/**
* Obtains a period for this chronology based on years, months and days.
* <p>
* This returns a period tied to the ISO chronology using the specified
* years, months and days. See {@link Period} for further details.
*
* @param years the number of years, may be negative
* @param months the number of years, may be negative
* @param days the number of years, may be negative
* @return the period in terms of this chronology, not null
* @return the ISO period, not null
*/
@Override // override with covariant return type
public Period period(int years, int months, int days) {
return Period.of(years, months, days);
}
/**
* Obtains a period for this chronology based on years, months and days.
* <p>
* This returns a period tied to the ISO chronology using the specified
* years, months and days. See {@link Period} for further details.
*
* @param years the number of years, may be negative
* @param months the number of years, may be negative
* @param days the number of years, may be negative
* @return the period in terms of this chronology, not null
* @return the ISO period, not null
*/
@Override // override with covariant return type
public Period period(int years, int months, int days) {
return Period.of(years, months, days);
}
/**
* Obtains a period for this chronology based on years, months and days.
* <p>
* This returns a period tied to the ISO chronology using the specified
* years, months and days. See {@link Period} for further details.
*
* @param years the number of years, may be negative
* @param months the number of years, may be negative
* @param days the number of years, may be negative
* @return the period in terms of this chronology, not null
* @return the ISO period, not null
*/
@Override // override with covariant return type
public Period period(int years, int months, int days) {
return Period.of(years, months, days);
}