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

The following examples show how to use java.time.temporal.ChronoUnit#DAYS . 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: XmlModel.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public static TemporalUnit convertToJavaTimeUnit(org.ehcache.xml.model.TimeUnit unit) {
  switch (unit) {
    case NANOS:
      return ChronoUnit.NANOS;
    case MICROS:
      return ChronoUnit.MICROS;
    case MILLIS:
      return ChronoUnit.MILLIS;
    case SECONDS:
      return ChronoUnit.SECONDS;
    case MINUTES:
      return ChronoUnit.MINUTES;
    case HOURS:
      return ChronoUnit.HOURS;
    case DAYS:
      return ChronoUnit.DAYS;
    default:
      throw new IllegalArgumentException("Unknown time unit: " + unit);
  }
}
 
Example 2
Source File: ExpiryUtils.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public static TemporalUnit jucTimeUnitToTemporalUnit(TimeUnit timeUnit) {
  switch (timeUnit) {
    case NANOSECONDS:
      return ChronoUnit.NANOS;
    case MICROSECONDS:
      return ChronoUnit.MICROS;
    case MILLISECONDS:
      return ChronoUnit.MILLIS;
    case SECONDS:
      return ChronoUnit.SECONDS;
    case MINUTES:
      return ChronoUnit.MINUTES;
    case HOURS:
      return ChronoUnit.HOURS;
    case DAYS:
      return ChronoUnit.DAYS;
    default:
      throw new AssertionError("Unkown TimeUnit: " + timeUnit);
  }
}
 
Example 3
Source File: TCKYear.java    From jdk8u-jdk 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 4
Source File: TCKYear.java    From jdk8u-jdk 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 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 6
Source File: ChronoUnits.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * Will be obsolete in Java 9 and can be replaced by {@code TimeUnit.toChronoUnit()}.
 * See: https://bugs.openjdk.java.net/browse/JDK-8141452
 *
 */
public static ChronoUnit createFromTimeUnit(final TimeUnit timeUnit) {
	switch (timeUnit) {
	case DAYS:
		return ChronoUnit.DAYS;
	case HOURS:
		return ChronoUnit.HOURS;
	case MINUTES:
		return ChronoUnit.MINUTES;
	case SECONDS:
		return ChronoUnit.SECONDS;
	case MILLISECONDS:
		return ChronoUnit.MILLIS;
	case MICROSECONDS:
		return ChronoUnit.MICROS;
	case NANOSECONDS:
		return ChronoUnit.NANOS;
	default:
		throw new IllegalArgumentException("Unknown TimeUnit constant");
	}
}
 
Example 7
Source File: TCKYear.java    From openjdk-jdk9 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 8
Source File: TCKYear.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@DataProvider
public static 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 9
Source File: TCKYear.java    From jdk8u60 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 10
Source File: TCKDayOfWeek.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[][] {
            {DayOfWeek.FRIDAY, TemporalQueries.chronology(), null},
            {DayOfWeek.FRIDAY, TemporalQueries.zoneId(), null},
            {DayOfWeek.FRIDAY, TemporalQueries.precision(), ChronoUnit.DAYS},
            {DayOfWeek.FRIDAY, TemporalQueries.zone(), null},
            {DayOfWeek.FRIDAY, TemporalQueries.offset(), null},
            {DayOfWeek.FRIDAY, TemporalQueries.localDate(), null},
            {DayOfWeek.FRIDAY, TemporalQueries.localTime(), null},
    };
}
 
Example 11
Source File: Period.java    From openjdk-jdk8u-backup 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 12
Source File: TCKLocalDate.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_2007_07_15, TemporalQueries.chronology(), IsoChronology.INSTANCE},
            {TEST_2007_07_15, TemporalQueries.zoneId(), null},
            {TEST_2007_07_15, TemporalQueries.precision(), ChronoUnit.DAYS},
            {TEST_2007_07_15, TemporalQueries.zone(), null},
            {TEST_2007_07_15, TemporalQueries.offset(), null},
            {TEST_2007_07_15, TemporalQueries.localDate(), TEST_2007_07_15},
            {TEST_2007_07_15, TemporalQueries.localTime(), null},
    };
}
 
Example 13
Source File: TCKPeriod.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] data_goodTemporalUnit() {
    return new Object[][] {
        {2, ChronoUnit.DAYS},
        {2, ChronoUnit.MONTHS},
        {2, ChronoUnit.YEARS},
    };
}
 
Example 14
Source File: TCKLocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="query")
Object[][] data_query() {
    return new Object[][] {
            {TEST_2007_07_15, TemporalQueries.chronology(), IsoChronology.INSTANCE},
            {TEST_2007_07_15, TemporalQueries.zoneId(), null},
            {TEST_2007_07_15, TemporalQueries.precision(), ChronoUnit.DAYS},
            {TEST_2007_07_15, TemporalQueries.zone(), null},
            {TEST_2007_07_15, TemporalQueries.offset(), null},
            {TEST_2007_07_15, TemporalQueries.localDate(), TEST_2007_07_15},
            {TEST_2007_07_15, TemporalQueries.localTime(), null},
    };
}
 
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: TimePeriodSpec.java    From java-timeseries with MIT License 4 votes vote down vote up
@Test
public void whenZeroPeriodLengthThenIllegalArgument() {
    exception.expect(IllegalArgumentException.class);
    new TimePeriod(ChronoUnit.DAYS, 0);
}
 
Example 17
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private MainPanel() {
  super(new GridLayout(0, 1));
  Calendar cal = Calendar.getInstance();
  cal.clear(Calendar.MILLISECOND);
  cal.clear(Calendar.SECOND);
  cal.clear(Calendar.MINUTE);
  cal.set(Calendar.HOUR_OF_DAY, 0);
  Date date = cal.getTime();
  cal.add(Calendar.DATE, -2);
  Date start = cal.getTime();
  cal.add(Calendar.DATE, 9);
  Date end = cal.getTime();

  System.out.println(date);
  System.out.println(start);
  System.out.println(end);

  String dateFormat = "yyyy/MM/dd";

  JSpinner spinner0 = new JSpinner(new SpinnerDateModel(date, start, end, Calendar.DAY_OF_MONTH));
  spinner0.setEditor(new JSpinner.DateEditor(spinner0, dateFormat));

  LocalDateTime d = LocalDateTime.now(ZoneId.systemDefault());
  LocalDateTime s = d.minus(2, ChronoUnit.DAYS);
  LocalDateTime e = d.plus(7, ChronoUnit.DAYS);

  System.out.println(d);
  System.out.println(s);
  System.out.println(e);

  JSpinner spinner1 = new JSpinner(new SpinnerDateModel(toDate(d), toDate(s), toDate(e), Calendar.DAY_OF_MONTH));
  spinner1.setEditor(new JSpinner.DateEditor(spinner1, dateFormat));

  JSpinner spinner2 = new JSpinner(new SpinnerLocalDateTimeModel(d, s, e, ChronoUnit.DAYS));
  spinner2.setEditor(new LocalDateTimeEditor(spinner2, dateFormat));

  // JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) spinner2.getEditor();
  // DefaultFormatter formatter = new InternationalFormatter(DateTimeFormatter.ofPattern(dateFormat).toFormat());
  // DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
  // JFormattedTextField ftf = editor.getTextField();
  // ftf.setHorizontalAlignment(SwingConstants.LEFT);
  // ftf.setColumns(10);
  // ftf.setEditable(true);
  // ftf.setFormatterFactory(factory);

  add(makeTitledPanel("SpinnerDateModel", spinner0));
  add(makeTitledPanel("SpinnerDateModel / toInstant", spinner1));
  add(makeTitledPanel("SpinnerLocalDateTimeModel", spinner2));
  setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 18
Source File: Period.java    From desugar_jdk_libs 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 19
Source File: Period.java    From j2objc 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 20
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);
    }
}