Java Code Examples for java.time.temporal.ChronoUnit#YEARS

The following examples show how to use java.time.temporal.ChronoUnit#YEARS . 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: TCKYear.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@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},
    };
}
 
Example 2
Source File: TCKYear.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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},
    };
}
 
Example 3
Source File: TCKDuration.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@DataProvider
public static Object[][] provider_factory_of_badTemporalUnit() {
    return new Object[][] {
        {0, MICROS},
        {0, MILLIS},
        {0, MINUTES},
        {0, HOURS},
        {0, HALF_DAYS},
        {0, DAYS},
        {0, ChronoUnit.MONTHS},
        {0, ChronoUnit.YEARS},
        {0, ChronoUnit.DECADES},
        {0, ChronoUnit.CENTURIES},
        {0, ChronoUnit.MILLENNIA},
    };
}
 
Example 4
Source File: TCKYear.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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},
    };
}
 
Example 5
Source File: TCKYear.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@DataProvider
public static 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},
    };
}
 
Example 6
Source File: Period.java    From desugar_jdk_libs with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance of {@code Period} from a temporal amount.
 * <p>
 * This obtains a period based on the specified amount.
 * A {@code TemporalAmount} represents an  amount of time, which may be
 * date-based or time-based, which this factory extracts to a {@code Period}.
 * <p>
 * The conversion loops around the set of units from the amount and uses
 * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
 * and {@link ChronoUnit#DAYS DAYS} units to create a period.
 * If any other units are found then an exception is thrown.
 * <p>
 * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
 *
 * @param amount  the temporal amount to convert, not null
 * @return the equivalent period, not null
 * @throws DateTimeException if unable to convert to a {@code Period}
 * @throws ArithmeticException if the amount of years, months or days exceeds an int
 */
public static Period from(TemporalAmount amount) {
    if (amount instanceof Period) {
        return (Period) amount;
    }
    if (amount instanceof ChronoPeriod) {
        if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) {
            throw new DateTimeException("Period requires ISO chronology: " + amount);
        }
    }
    Objects.requireNonNull(amount, "amount");
    int years = 0;
    int months = 0;
    int days = 0;
    for (TemporalUnit unit : amount.getUnits()) {
        long unitAmount = amount.get(unit);
        if (unit == ChronoUnit.YEARS) {
            years = Math8.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.MONTHS) {
            months = Math8.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.DAYS) {
            days = Math8.toIntExact(unitAmount);
        } else {
            throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit);
        }
    }
    return create(years, months, days);
}
 
Example 7
Source File: ChronoPeriodImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long get(TemporalUnit unit) {
    if (unit == ChronoUnit.YEARS) {
        return years;
    } else if (unit == ChronoUnit.MONTHS) {
        return months;
    } else if (unit == ChronoUnit.DAYS) {
        return days;
    } else {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
}
 
Example 8
Source File: Period.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance of {@code Period} from a temporal amount.
 * <p>
 * This obtains a period based on the specified amount.
 * A {@code TemporalAmount} represents an  amount of time, which may be
 * date-based or time-based, which this factory extracts to a {@code Period}.
 * <p>
 * The conversion loops around the set of units from the amount and uses
 * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
 * and {@link ChronoUnit#DAYS DAYS} units to create a period.
 * If any other units are found then an exception is thrown.
 * <p>
 * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
 *
 * @param amount  the temporal amount to convert, not null
 * @return the equivalent period, not null
 * @throws DateTimeException if unable to convert to a {@code Period}
 * @throws ArithmeticException if the amount of years, months or days exceeds an int
 */
public static Period from(TemporalAmount amount) {
    if (amount instanceof Period) {
        return (Period) amount;
    }
    if (amount instanceof ChronoPeriod) {
        if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) {
            throw new DateTimeException("Period requires ISO chronology: " + amount);
        }
    }
    Objects.requireNonNull(amount, "amount");
    int years = 0;
    int months = 0;
    int days = 0;
    for (TemporalUnit unit : amount.getUnits()) {
        long unitAmount = amount.get(unit);
        if (unit == ChronoUnit.YEARS) {
            years = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.MONTHS) {
            months = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.DAYS) {
            days = Math.toIntExact(unitAmount);
        } else {
            throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit);
        }
    }
    return create(years, months, days);
}
 
Example 9
Source File: TCKYear.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="query")
Object[][] data_query() {
    return new Object[][] {
            {TEST_2008, TemporalQueries.chronology(), IsoChronology.INSTANCE},
            {TEST_2008, TemporalQueries.zoneId(), null},
            {TEST_2008, TemporalQueries.precision(), ChronoUnit.YEARS},
            {TEST_2008, TemporalQueries.zone(), null},
            {TEST_2008, TemporalQueries.offset(), null},
            {TEST_2008, TemporalQueries.localDate(), null},
            {TEST_2008, TemporalQueries.localTime(), null},
    };
}
 
Example 10
Source File: TCKYear.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="query")
Object[][] data_query() {
    return new Object[][] {
            {TEST_2008, TemporalQueries.chronology(), IsoChronology.INSTANCE},
            {TEST_2008, TemporalQueries.zoneId(), null},
            {TEST_2008, TemporalQueries.precision(), ChronoUnit.YEARS},
            {TEST_2008, TemporalQueries.zone(), null},
            {TEST_2008, TemporalQueries.offset(), null},
            {TEST_2008, TemporalQueries.localDate(), null},
            {TEST_2008, TemporalQueries.localTime(), null},
    };
}
 
Example 11
Source File: ChronoPeriodImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long get(TemporalUnit unit) {
    if (unit == ChronoUnit.YEARS) {
        return years;
    } else if (unit == ChronoUnit.MONTHS) {
        return months;
    } else if (unit == ChronoUnit.DAYS) {
        return days;
    } else {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
}
 
Example 12
Source File: TCKYear.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="query")
Object[][] data_query() {
    return new Object[][] {
            {TEST_2008, TemporalQueries.chronology(), IsoChronology.INSTANCE},
            {TEST_2008, TemporalQueries.zoneId(), null},
            {TEST_2008, TemporalQueries.precision(), ChronoUnit.YEARS},
            {TEST_2008, TemporalQueries.zone(), null},
            {TEST_2008, TemporalQueries.offset(), null},
            {TEST_2008, TemporalQueries.localDate(), null},
            {TEST_2008, TemporalQueries.localTime(), null},
    };
}
 
Example 13
Source File: TCKYearMonth.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="minus_long_TemporalUnit")
Object[][] data_minus_long_TemporalUnit() {
    return new Object[][] {
        {YearMonth.of(1, 10), 1, ChronoUnit.YEARS, YearMonth.of(0, 10), null},
        {YearMonth.of(1, 10), 12, ChronoUnit.YEARS, YearMonth.of(-11, 10), null},
        {YearMonth.of(1, 10), 0, ChronoUnit.YEARS, YearMonth.of(1, 10), null},
        {YearMonth.of(999999999, 12), 0, ChronoUnit.YEARS, YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), 0, ChronoUnit.YEARS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), 999999999, ChronoUnit.YEARS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), -999999999, ChronoUnit.YEARS, YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 10), 1, ChronoUnit.MONTHS, YearMonth.of(1, 9), null},
        {YearMonth.of(1, 10), 12, ChronoUnit.MONTHS, YearMonth.of(0, 10), null},
        {YearMonth.of(1, 10), 0, ChronoUnit.MONTHS, YearMonth.of(1, 10), null},
        {YearMonth.of(999999999, 12), 0, ChronoUnit.MONTHS, YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), 0, ChronoUnit.MONTHS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), 1, ChronoUnit.MONTHS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 11), -1, ChronoUnit.MONTHS, YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 10), 1, ChronoUnit.ERAS, YearMonth.of(0, 10), null},
        {YearMonth.of(5, 10), 1, ChronoUnit.CENTURIES, YearMonth.of(-95, 10), null},
        {YearMonth.of(5, 10), 1, ChronoUnit.DECADES, YearMonth.of(-5, 10), null},

        {YearMonth.of(999999999, 12), -1, ChronoUnit.MONTHS, null, DateTimeException.class},
        {YearMonth.of(-999999999, 1), 1, ChronoUnit.MONTHS, null, DateTimeException.class},

        {YearMonth.of(1, 1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
        {YearMonth.of(1, 1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
    };
}
 
Example 14
Source File: TCKYearMonth.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="plus_long_TemporalUnit")
Object[][] data_plus_long_TemporalUnit() {
    return new Object[][] {
        {YearMonth.of(1, 10), 1, ChronoUnit.YEARS, YearMonth.of(2, 10), null},
        {YearMonth.of(1, 10), -12, ChronoUnit.YEARS, YearMonth.of(-11, 10), null},
        {YearMonth.of(1, 10), 0, ChronoUnit.YEARS, YearMonth.of(1, 10), null},
        {YearMonth.of(999999999, 12), 0, ChronoUnit.YEARS, YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), 0, ChronoUnit.YEARS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), -999999999, ChronoUnit.YEARS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), 999999999, ChronoUnit.YEARS, YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 10), 1, ChronoUnit.MONTHS, YearMonth.of(1, 11), null},
        {YearMonth.of(1, 10), -12, ChronoUnit.MONTHS, YearMonth.of(0, 10), null},
        {YearMonth.of(1, 10), 0, ChronoUnit.MONTHS, YearMonth.of(1, 10), null},
        {YearMonth.of(999999999, 12), 0, ChronoUnit.MONTHS, YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), 0, ChronoUnit.MONTHS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), -1, ChronoUnit.MONTHS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 3), 9, ChronoUnit.MONTHS, YearMonth.of(999999999, 12), null},

        {YearMonth.of(-1, 10), 1, ChronoUnit.ERAS, YearMonth.of(2, 10), null},
        {YearMonth.of(5, 10), 1, ChronoUnit.CENTURIES, YearMonth.of(105, 10), null},
        {YearMonth.of(5, 10), 1, ChronoUnit.DECADES, YearMonth.of(15, 10), null},

        {YearMonth.of(999999999, 12), 1, ChronoUnit.MONTHS, null, DateTimeException.class},
        {YearMonth.of(-999999999, 1), -1, ChronoUnit.MONTHS, null, DateTimeException.class},

        {YearMonth.of(1, 1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
        {YearMonth.of(1, 1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
    };
}
 
Example 15
Source File: TCKYearMonth.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] data_minus_long_TemporalUnit() {
    return new Object[][] {
        {YearMonth.of(1, 10), 1, ChronoUnit.YEARS, YearMonth.of(0, 10), null},
        {YearMonth.of(1, 10), 12, ChronoUnit.YEARS, YearMonth.of(-11, 10), null},
        {YearMonth.of(1, 10), 0, ChronoUnit.YEARS, YearMonth.of(1, 10), null},
        {YearMonth.of(999999999, 12), 0, ChronoUnit.YEARS, YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), 0, ChronoUnit.YEARS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), 999999999, ChronoUnit.YEARS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), -999999999, ChronoUnit.YEARS, YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 10), 1, ChronoUnit.MONTHS, YearMonth.of(1, 9), null},
        {YearMonth.of(1, 10), 12, ChronoUnit.MONTHS, YearMonth.of(0, 10), null},
        {YearMonth.of(1, 10), 0, ChronoUnit.MONTHS, YearMonth.of(1, 10), null},
        {YearMonth.of(999999999, 12), 0, ChronoUnit.MONTHS, YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), 0, ChronoUnit.MONTHS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), 1, ChronoUnit.MONTHS, YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 11), -1, ChronoUnit.MONTHS, YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 10), 1, ChronoUnit.ERAS, YearMonth.of(0, 10), null},
        {YearMonth.of(5, 10), 1, ChronoUnit.CENTURIES, YearMonth.of(-95, 10), null},
        {YearMonth.of(5, 10), 1, ChronoUnit.DECADES, YearMonth.of(-5, 10), null},

        {YearMonth.of(999999999, 12), -1, ChronoUnit.MONTHS, null, DateTimeException.class},
        {YearMonth.of(-999999999, 1), 1, ChronoUnit.MONTHS, null, DateTimeException.class},

        {YearMonth.of(1, 1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
        {YearMonth.of(1, 1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
    };
}
 
Example 16
Source File: FindResult.java    From find with MIT License 5 votes vote down vote up
@SuppressWarnings("TypeMayBeWeakened")
private ZonedDateTime parseRelativeTime(final ZonedDateTime timeNow, final int timeAmount, final String timeUnit) {
    TemporalUnit temporalUnit = ChronoUnit.SECONDS;
    switch (timeUnit) {
        case "minute":
        case "minutes":
            temporalUnit = ChronoUnit.MINUTES;
            break;

        case "hour":
        case "hours":
            temporalUnit = ChronoUnit.HOURS;
            break;

        case "day":
        case "days":
            temporalUnit = ChronoUnit.DAYS;
            break;

        case "month":
        case "months":
            temporalUnit = ChronoUnit.MONTHS;
            break;

        case "year":
        case "years":
            temporalUnit = ChronoUnit.YEARS;
            break;
    }

    return timeNow.plus(timeAmount, temporalUnit);
}
 
Example 17
Source File: Period.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the requested unit.
 * <p>
 * This returns a value for each of the three supported units,
 * {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS} and
 * {@link ChronoUnit#DAYS DAYS}.
 * All other units throw an exception.
 *
 * @param unit the {@code TemporalUnit} for which to return the value
 * @return the long value of the unit
 * @throws DateTimeException if the unit is not supported
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
@Override
public long get(TemporalUnit unit) {
    if (unit == ChronoUnit.YEARS) {
        return getYears();
    } else if (unit == ChronoUnit.MONTHS) {
        return getMonths();
    } else if (unit == ChronoUnit.DAYS) {
        return getDays();
    } else {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
}
 
Example 18
Source File: Period.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the value of the requested unit.
 * <p>
 * This returns a value for each of the three supported units,
 * {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS} and
 * {@link ChronoUnit#DAYS DAYS}.
 * All other units throw an exception.
 *
 * @param unit the {@code TemporalUnit} for which to return the value
 * @return the long value of the unit
 * @throws DateTimeException if the unit is not supported
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
@Override
public long get(TemporalUnit unit) {
    if (unit == ChronoUnit.YEARS) {
        return getYears();
    } else if (unit == ChronoUnit.MONTHS) {
        return getMonths();
    } else if (unit == ChronoUnit.DAYS) {
        return getDays();
    } else {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
}
 
Example 19
Source File: Period.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the requested unit.
 * <p>
 * This returns a value for each of the three supported units,
 * {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS} and
 * {@link ChronoUnit#DAYS DAYS}.
 * All other units throw an exception.
 *
 * @param unit the {@code TemporalUnit} for which to return the value
 * @return the long value of the unit
 * @throws DateTimeException if the unit is not supported
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
@Override
public long get(TemporalUnit unit) {
    if (unit == ChronoUnit.YEARS) {
        return getYears();
    } else if (unit == ChronoUnit.MONTHS) {
        return getMonths();
    } else if (unit == ChronoUnit.DAYS) {
        return getDays();
    } else {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
}
 
Example 20
Source File: TimePeriod.java    From java-timeseries with MIT License 2 votes vote down vote up
/**
 * Create and return a new TimePeriod representing one half of a decade.
 *
 * @return a new TimePeriod representing one half of a decade.
 */
public static TimePeriod halfDecade() {
  return new TimePeriod(ChronoUnit.YEARS, 5);
}