Java Code Examples for java.time.temporal.TemporalField#getFrom()

The following examples show how to use java.time.temporal.TemporalField#getFrom() . 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: MinguoDate.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case PROLEPTIC_MONTH:
                return getProlepticMonth();
            case YEAR_OF_ERA: {
                int prolepticYear = getProlepticYear();
                return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
            }
            case YEAR:
                return getProlepticYear();
            case ERA:
                return (getProlepticYear() >= 1 ? 1 : 0);
        }
        return isoDate.getLong(field);
    }
    return field.getFrom(this);
}
 
Example 2
Source File: MinguoDate.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case PROLEPTIC_MONTH:
                return getProlepticMonth();
            case YEAR_OF_ERA: {
                int prolepticYear = getProlepticYear();
                return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
            }
            case YEAR:
                return getProlepticYear();
            case ERA:
                return (getProlepticYear() >= 1 ? 1 : 0);
        }
        return isoDate.getLong(field);
    }
    return field.getFrom(this);
}
 
Example 3
Source File: ChronoLocalDateTimeImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        return (f.isTimeBased() ? time.getLong(field) : date.getLong(field));
    }
    return field.getFrom(this);
}
 
Example 4
Source File: ZoneOffset.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this offset as a {@code long}.
 * <p>
 * This queries this offset for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@code OFFSET_SECONDS} field returns the value of the offset.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field == OFFSET_SECONDS) {
        return totalSeconds;
    } else if (field instanceof ChronoField) {
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example 5
Source File: YearMonth.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this year-month as a {@code long}.
 * <p>
 * This queries this year-month for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this year-month.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case MONTH_OF_YEAR: return month;
            case PROLEPTIC_MONTH: return getProlepticMonth();
            case YEAR_OF_ERA: return (year < 1 ? 1 - year : year);
            case YEAR: return year;
            case ERA: return (year < 1 ? 0 : 1);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example 6
Source File: OffsetDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this date-time as a {@code long}.
 * <p>
 * This queries this date-time for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this date-time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case INSTANT_SECONDS: return toEpochSecond();
            case OFFSET_SECONDS: return getOffset().getTotalSeconds();
        }
        return dateTime.getLong(field);
    }
    return field.getFrom(this);
}
 
Example 7
Source File: LocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this date as a {@code long}.
 * <p>
 * This queries this date for the value for the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this date.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        if (field == EPOCH_DAY) {
            return toEpochDay();
        }
        if (field == PROLEPTIC_MONTH) {
            return getProlepticMonth();
        }
        return get0(field);
    }
    return field.getFrom(this);
}
 
Example 8
Source File: LocalDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this date-time as a {@code long}.
 * <p>
 * This queries this date-time for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this date-time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        return (f.isTimeBased() ? time.getLong(field) : date.getLong(field));
    }
    return field.getFrom(this);
}
 
Example 9
Source File: OffsetTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this time as a {@code long}.
 * <p>
 * This queries this time for the value for the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        if (field == OFFSET_SECONDS) {
            return offset.getTotalSeconds();
        }
        return time.getLong(field);
    }
    return field.getFrom(this);
}
 
Example 10
Source File: Year.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this year as a {@code long}.
 * <p>
 * This queries this year for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this year.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case YEAR_OF_ERA: return (year < 1 ? 1 - year : year);
            case YEAR: return year;
            case ERA: return (year < 1 ? 0 : 1);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example 11
Source File: OffsetDateTime.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this date-time as a {@code long}.
 * <p>
 * This queries this date-time for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this date-time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case INSTANT_SECONDS: return toEpochSecond();
            case OFFSET_SECONDS: return getOffset().getTotalSeconds();
        }
        return dateTime.getLong(field);
    }
    return field.getFrom(this);
}
 
Example 12
Source File: ZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this date-time as a {@code long}.
 * <p>
 * This queries this date-time for the value for the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this date-time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case INSTANT_SECONDS: return toEpochSecond();
            case OFFSET_SECONDS: return getOffset().getTotalSeconds();
        }
        return dateTime.getLong(field);
    }
    return field.getFrom(this);
}
 
Example 13
Source File: LocalTime.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this time as a {@code long}.
 * <p>
 * This queries this time for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        if (field == NANO_OF_DAY) {
            return toNanoOfDay();
        }
        if (field == MICRO_OF_DAY) {
            return toNanoOfDay() / 1000;
        }
        return get0(field);
    }
    return field.getFrom(this);
}
 
Example 14
Source File: OffsetDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this date-time as a {@code long}.
 * <p>
 * This queries this date-time for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this date-time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case INSTANT_SECONDS: return toEpochSecond();
            case OFFSET_SECONDS: return getOffset().getTotalSeconds();
        }
        return dateTime.getLong(field);
    }
    return field.getFrom(this);
}
 
Example 15
Source File: OffsetTime.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this time as a {@code long}.
 * <p>
 * This queries this time for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        if (field == OFFSET_SECONDS) {
            return offset.getTotalSeconds();
        }
        return time.getLong(field);
    }
    return field.getFrom(this);
}
 
Example 16
Source File: ZoneOffset.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this offset as a {@code long}.
 * <p>
 * This queries this offset for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@code OFFSET_SECONDS} field returns the value of the offset.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field == OFFSET_SECONDS) {
        return totalSeconds;
    } else if (field instanceof ChronoField) {
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example 17
Source File: LocalTime.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this time as a {@code long}.
 * <p>
 * This queries this time for the value for the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this time.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        if (field == NANO_OF_DAY) {
            return toNanoOfDay();
        }
        if (field == MICRO_OF_DAY) {
            return toNanoOfDay() / 1000;
        }
        return get0(field);
    }
    return field.getFrom(this);
}
 
Example 18
Source File: LocalDate.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Gets the value of the specified field from this date as a {@code long}.
 * <p>
 * This queries this date for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this date.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        if (field == EPOCH_DAY) {
            return toEpochDay();
        }
        if (field == PROLEPTIC_MONTH) {
            return getProlepticMonth();
        }
        return get0(field);
    }
    return field.getFrom(this);
}
 
Example 19
Source File: YearMonth.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this year-month as a {@code long}.
 * <p>
 * This queries this year-month for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this year-month.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case MONTH_OF_YEAR: return month;
            case PROLEPTIC_MONTH: return getProlepticMonth();
            case YEAR_OF_ERA: return (year < 1 ? 1 - year : year);
            case YEAR: return year;
            case ERA: return (year < 1 ? 0 : 1);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example 20
Source File: LocalDate.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of the specified field from this date as a {@code long}.
 * <p>
 * This queries this date for the value of the specified field.
 * If it is not possible to return the value, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return valid
 * values based on this date.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 * passing {@code this} as the argument. Whether the value can be obtained,
 * and what the value represents, is determined by the field.
 *
 * @param field  the field to get, not null
 * @return the value for the field
 * @throws DateTimeException if a value for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        if (field == EPOCH_DAY) {
            return toEpochDay();
        }
        if (field == PROLEPTIC_MONTH) {
            return getProlepticMonth();
        }
        return get0(field);
    }
    return field.getFrom(this);
}