Java Code Examples for java.time.temporal.ValueRange#checkValidValue()

The following examples show how to use java.time.temporal.ValueRange#checkValidValue() . 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-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param prolepticYear  the Coptic proleptic-year
 * @param month  the Coptic month, from 1 to 13
 * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
 * @throws DateTimeException if the date is invalid
 */
CopticDate(int prolepticYear, int month, int dayOfMonth) {
    CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
    ValueRange range;
    if (month == 13) {
        range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
    } else {
        range = CopticChronology.DOM_RANGE;
    }
    range.checkValidValue(dayOfMonth, DAY_OF_MONTH);

    this.prolepticYear = prolepticYear;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
 
Example 2
Source File: CopticDate.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param prolepticYear  the Coptic proleptic-year
 * @param month  the Coptic month, from 1 to 13
 * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
 * @throws DateTimeException if the date is invalid
 */
CopticDate(int prolepticYear, int month, int dayOfMonth) {
    CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
    ValueRange range;
    if (month == 13) {
        range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
    } else {
        range = CopticChronology.DOM_RANGE;
    }
    range.checkValidValue(dayOfMonth, DAY_OF_MONTH);

    this.prolepticYear = prolepticYear;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
 
Example 3
Source File: CopticDate.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param prolepticYear  the Coptic proleptic-year
 * @param month  the Coptic month, from 1 to 13
 * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
 * @throws DateTimeException if the date is invalid
 */
CopticDate(int prolepticYear, int month, int dayOfMonth) {
    CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
    ValueRange range;
    if (month == 13) {
        range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
    } else {
        range = CopticChronology.DOM_RANGE;
    }
    range.checkValidValue(dayOfMonth, DAY_OF_MONTH);

    this.prolepticYear = prolepticYear;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
 
Example 4
Source File: CopticDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param prolepticYear  the Coptic proleptic-year
 * @param month  the Coptic month, from 1 to 13
 * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
 * @throws DateTimeException if the date is invalid
 */
CopticDate(int prolepticYear, int month, int dayOfMonth) {
    CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
    ValueRange range;
    if (month == 13) {
        range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
    } else {
        range = CopticChronology.DOM_RANGE;
    }
    range.checkValidValue(dayOfMonth, DAY_OF_MONTH);

    this.prolepticYear = prolepticYear;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
 
Example 5
Source File: CopticDate.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param prolepticYear  the Coptic proleptic-year
 * @param month  the Coptic month, from 1 to 13
 * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
 * @throws DateTimeException if the date is invalid
 */
CopticDate(int prolepticYear, int month, int dayOfMonth) {
    CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
    ValueRange range;
    if (month == 13) {
        range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
    } else {
        range = CopticChronology.DOM_RANGE;
    }
    range.checkValidValue(dayOfMonth, DAY_OF_MONTH);

    this.prolepticYear = prolepticYear;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
 
Example 6
Source File: CopticDate.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param prolepticYear  the Coptic proleptic-year
 * @param month  the Coptic month, from 1 to 13
 * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
 * @throws DateTimeException if the date is invalid
 */
CopticDate(int prolepticYear, int month, int dayOfMonth) {
    CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
    ValueRange range;
    if (month == 13) {
        range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
    } else {
        range = CopticChronology.DOM_RANGE;
    }
    range.checkValidValue(dayOfMonth, DAY_OF_MONTH);

    this.prolepticYear = prolepticYear;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
 
Example 7
Source File: CopticDate.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param prolepticYear  the Coptic proleptic-year
 * @param month  the Coptic month, from 1 to 13
 * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
 * @throws DateTimeException if the date is invalid
 */
CopticDate(int prolepticYear, int month, int dayOfMonth) {
    CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
    ValueRange range;
    if (month == 13) {
        range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
    } else {
        range = CopticChronology.DOM_RANGE;
    }
    range.checkValidValue(dayOfMonth, DAY_OF_MONTH);

    this.prolepticYear = prolepticYear;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
 
Example 8
Source File: CopticDate.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param prolepticYear  the Coptic proleptic-year
 * @param month  the Coptic month, from 1 to 13
 * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
 * @throws DateTimeException if the date is invalid
 */
CopticDate(int prolepticYear, int month, int dayOfMonth) {
    CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
    ValueRange range;
    if (month == 13) {
        range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
    } else {
        range = CopticChronology.DOM_RANGE;
    }
    range.checkValidValue(dayOfMonth, DAY_OF_MONTH);

    this.prolepticYear = prolepticYear;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
 
Example 9
Source File: DateTimeFormatterBuilder.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 10
Source File: DateTimeFormatterBuilder.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 11
Source File: DateTimeFormatterBuilder.java    From desugar_jdk_libs with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 12
Source File: DateTimeFormatterBuilder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 13
Source File: DateTimeFormatterBuilder.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 14
Source File: DateTimeFormatterBuilder.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 15
Source File: DateTimeFormatterBuilder.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 16
Source File: DateTimeFormatterBuilder.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 17
Source File: DateTimeFormatterBuilder.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 18
Source File: DateTimeFormatterBuilder.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 19
Source File: DateTimeFormatterBuilder.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}
 
Example 20
Source File: DateTimeFormatterBuilder.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts a value for this field to a fraction between 0 and 1.
 * <p>
 * The fractional value is between 0 (inclusive) and 1 (exclusive).
 * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
 * The fraction is obtained by calculation from the field range using 9 decimal
 * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
 * The calculation is inaccurate if the values do not run continuously from smallest to largest.
 * <p>
 * For example, the second-of-minute value of 15 would be returned as 0.25,
 * assuming the standard definition of 60 seconds in a minute.
 *
 * @param value  the value to convert, must be valid for this rule
 * @return the value as a fraction within the range, from 0 to 1, not null
 * @throws DateTimeException if the value cannot be converted to a fraction
 */
private BigDecimal convertToFraction(long value) {
    ValueRange range = field.range();
    range.checkValidValue(value, field);
    BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
    BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
    BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
    BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
    // stripTrailingZeros bug
    return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
}