当前位置: 首页>>代码示例>>Java>>正文


Java ChronoField.checkValidValue方法代码示例

本文整理汇总了Java中java.time.temporal.ChronoField.checkValidValue方法的典型用法代码示例。如果您正苦于以下问题:Java ChronoField.checkValidValue方法的具体用法?Java ChronoField.checkValidValue怎么用?Java ChronoField.checkValidValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.time.temporal.ChronoField的用法示例。


在下文中一共展示了ChronoField.checkValidValue方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: with

import java.time.temporal.ChronoField; //导入方法依赖的package包/类
@Override
public CopticDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);        // TODO: validate value
        int nvalue = (int) newValue;
        switch (f) {
            case DAY_OF_WEEK: return plusDays(newValue - get(ChronoField.DAY_OF_WEEK));
            case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
            case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
            case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, month, nvalue);
            case DAY_OF_YEAR: return resolvePreviousValid(prolepticYear, ((nvalue - 1) / 30) + 1, ((nvalue - 1) % 30) + 1);
            case EPOCH_DAY: return ofEpochDay(nvalue);
            case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
            case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
            case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, day);
            case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, month, day);
            case YEAR: return resolvePreviousValid(nvalue, month, day);
            case ERA: return resolvePreviousValid(1 - prolepticYear, month, day);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:25,代码来源:CopticDate.java

示例2: with

import java.time.temporal.ChronoField; //导入方法依赖的package包/类
/**
 * Returns a copy of this year-month with the specified field set to a new value.
 * <p>
 * This returns a {@code YearMonth}, based on this one, with the value
 * for the specified field changed.
 * This can be used to change any supported field, such as the year or month.
 * If it is not possible to set 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 adjustment is implemented here.
 * The supported fields behave as follows:
 * <ul>
 * <li>{@code MONTH_OF_YEAR} -
 *  Returns a {@code YearMonth} with the specified month-of-year.
 *  The year will be unchanged.
 * <li>{@code PROLEPTIC_MONTH} -
 *  Returns a {@code YearMonth} with the specified proleptic-month.
 *  This completely replaces the year and month of this object.
 * <li>{@code YEAR_OF_ERA} -
 *  Returns a {@code YearMonth} with the specified year-of-era
 *  The month and era will be unchanged.
 * <li>{@code YEAR} -
 *  Returns a {@code YearMonth} with the specified year.
 *  The month will be unchanged.
 * <li>{@code ERA} -
 *  Returns a {@code YearMonth} with the specified era.
 *  The month and year-of-era will be unchanged.
 * </ul>
 * <p>
 * In all cases, if the new value is outside the valid range of values for the field
 * then a {@code DateTimeException} will be thrown.
 * <p>
 * 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.adjustInto(Temporal, long)}
 * passing {@code this} as the argument. In this case, the field determines
 * whether and how to adjust the instant.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param field  the field to set in the result, not null
 * @param newValue  the new value of the field in the result
 * @return a {@code YearMonth} based on {@code this} with the specified field set, not null
 * @throws DateTimeException if the field cannot be set
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public YearMonth with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch (f) {
            case MONTH_OF_YEAR: return withMonth((int) newValue);
            case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
            case YEAR_OF_ERA: return withYear((int) (year < 1 ? 1 - newValue : newValue));
            case YEAR: return withYear((int) newValue);
            case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:65,代码来源:YearMonth.java

示例3: with

import java.time.temporal.ChronoField; //导入方法依赖的package包/类
/**
 * Returns a copy of this year with the specified field set to a new value.
 * <p>
 * This returns a {@code Year}, based on this one, with the value
 * for the specified field changed.
 * If it is not possible to set 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 adjustment is implemented here.
 * The supported fields behave as follows:
 * <ul>
 * <li>{@code YEAR_OF_ERA} -
 *  Returns a {@code Year} with the specified year-of-era
 *  The era will be unchanged.
 * <li>{@code YEAR} -
 *  Returns a {@code Year} with the specified year.
 *  This completely replaces the date and is equivalent to {@link #of(int)}.
 * <li>{@code ERA} -
 *  Returns a {@code Year} with the specified era.
 *  The year-of-era will be unchanged.
 * </ul>
 * <p>
 * In all cases, if the new value is outside the valid range of values for the field
 * then a {@code DateTimeException} will be thrown.
 * <p>
 * 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.adjustInto(Temporal, long)}
 * passing {@code this} as the argument. In this case, the field determines
 * whether and how to adjust the instant.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param field  the field to set in the result, not null
 * @param newValue  the new value of the field in the result
 * @return a {@code Year} based on {@code this} with the specified field set, not null
 * @throws DateTimeException if the field cannot be set
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Year with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch (f) {
            case YEAR_OF_ERA: return Year.of((int) (year < 1 ? 1 - newValue : newValue));
            case YEAR: return Year.of((int) newValue);
            case ERA: return (getLong(ERA) == newValue ? this : Year.of(1 - year));
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:56,代码来源:Year.java


注:本文中的java.time.temporal.ChronoField.checkValidValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。