Java Code Examples for java.time.Period#of()

The following examples show how to use java.time.Period#of() . 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 check out the related API usage on the sidebar.
Example 1
Source File: CopticDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: XmlAdaptersTest.java    From threeten-jaxb with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example 3
Source File: TCKChronoPeriod.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: TCKYear.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@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)},
    };
}
 
Example 5
Source File: TCKYear.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@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)},
    };
}
 
Example 6
Source File: TCKPeriod.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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"},
    };
}
 
Example 7
Source File: TCKYear.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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)},
    };
}
 
Example 8
Source File: PeriodDeserTest.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: TCKPeriod.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void test_equals_otherClass() {
    Period test = Period.of(1, 2, 3);
    assertEquals(test.equals(""), false);
}
 
Example 10
Source File: PeriodHandle.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
public Period readResolve() {
    return Period.of(years, months, days);
}
 
Example 11
Source File: TCKPeriod.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@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)},
    };
}
 
Example 12
Source File: TCKPeriod.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="BadTemporalUnit", expectedExceptions=DateTimeException.class)
public void test_bad_getUnit(TemporalUnit unit) {
    Period period = Period.of(2, 2, 2);
    period.get(unit);
}
 
Example 13
Source File: TCKPeriod.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void test_equals_self() {
    Period test = Period.of(1, 2, 3);
    assertEquals(test.equals(test), true);
}
 
Example 14
Source File: TCKPeriod.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void factory_from_TemporalAmount_Period() {
    TemporalAmount amount = Period.of(1, 2, 3);
    assertPeriod(Period.from(amount), 1, 2, 3);
}
 
Example 15
Source File: TCKPeriod.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static Period pymd(int y, int m, int d) {
    return Period.of(y, m, d);
}
 
Example 16
Source File: TCKPeriod.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void test_equals_otherClass() {
    Period test = Period.of(1, 2, 3);
    assertEquals(test.equals(""), false);
}
 
Example 17
Source File: IsoChronology.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: IsoChronology.java    From jdk8u-jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: IsoChronology.java    From desugar_jdk_libs with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: IsoChronology.java    From Java8CN with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}