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


Java DateTimeValueRange.isFixed方法代码示例

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


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

示例1: doPlusAdjustment

import java.time.calendrical.DateTimeValueRange; //导入方法依赖的package包/类
/**
 * Adds this period to the specified date-time object.
 * <p>
 * This method is not intended to be called by application code directly. Applications should use the
 * {@code plus(PlusAdjuster)} method on the date-time object passing this period as the argument.
 * <p>
 * The calculation will add the years, then months, then days, then nanos. Only non-zero amounts will be
 * added. If the date-time has a calendar system with a fixed number of months in a year, then the years and
 * months will be combined before being added.
 * 
 * @param dateTime the date-time object to adjust, not null
 * @return an object of the same type with the adjustment made, not null
 * @throws DateTimeException if unable to add
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public DateTime doPlusAdjustment(DateTime dateTime) {

  if ((this.years | this.months) != 0) {
    DateTimeValueRange startRange = Chrono.from(dateTime).range(MONTH_OF_YEAR);
    if (startRange.isFixed() && startRange.isIntValue()) {
      long monthCount = startRange.getMaximum() - startRange.getMinimum() + 1;
      dateTime = dateTime.plus(this.years * monthCount + this.months, MONTHS);
    } else {
      if (this.years != 0) {
        dateTime = dateTime.plus(this.years, YEARS);
      }
      if (this.months != 0) {
        dateTime = dateTime.plus(this.months, MONTHS);
      }
    }
  }
  if (this.days != 0) {
    dateTime = dateTime.plus(this.days, DAYS);
  }
  if (this.nanos != 0) {
    dateTime = dateTime.plus(this.nanos, NANOS);
  }
  return dateTime;
}
 
开发者ID:kiegroup,项目名称:optashift-employee-rostering,代码行数:41,代码来源:Period.java

示例2: doMinusAdjustment

import java.time.calendrical.DateTimeValueRange; //导入方法依赖的package包/类
/**
 * Subtracts this period from the specified date-time object.
 * <p>
 * This method is not intended to be called by application code directly. Applications should use the
 * {@code minus(MinusAdjuster)} method on the date-time object passing this period as the argument.
 * <p>
 * The calculation will subtract the years, then months, then days, then nanos. Only non-zero amounts will
 * be subtracted. If the date-time has a calendar system with a fixed number of months in a year, then the
 * years and months will be combined before being subtracted.
 * 
 * @param dateTime the date-time object to adjust, not null
 * @return an object of the same type with the adjustment made, not null
 * @throws DateTimeException if unable to subtract
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public DateTime doMinusAdjustment(DateTime dateTime) {

  if ((this.years | this.months) != 0) {
    DateTimeValueRange startRange = Chrono.from(dateTime).range(MONTH_OF_YEAR);
    if (startRange.isFixed() && startRange.isIntValue()) {
      long monthCount = startRange.getMaximum() - startRange.getMinimum() + 1;
      dateTime = dateTime.minus(this.years * monthCount + this.months, MONTHS);
    } else {
      if (this.years != 0) {
        dateTime = dateTime.minus(this.years, YEARS);
      }
      if (this.months != 0) {
        dateTime = dateTime.minus(this.months, MONTHS);
      }
    }
  }
  if (this.days != 0) {
    dateTime = dateTime.minus(this.days, DAYS);
  }
  if (this.nanos != 0) {
    dateTime = dateTime.minus(this.nanos, NANOS);
  }
  return dateTime;
}
 
开发者ID:kiegroup,项目名称:optashift-employee-rostering,代码行数:41,代码来源:Period.java

示例3: between

import java.time.calendrical.DateTimeValueRange; //导入方法依赖的package包/类
/**
 * Returns a {@code Period} consisting of the number of years, months, days, hours, minutes, seconds, and
 * nanoseconds between two {@code DateTimeAccessor} instances.
 * <p>
 * The start date is included, but the end date is not. Only whole years count. For example, from
 * {@code 2010-01-15} to {@code 2011-03-18} is one year, two months and three days.
 * <p>
 * This method examines the {@link ChronoField fields} {@code YEAR}, {@code MONTH_OF_YEAR},
 * {@code DAY_OF_MONTH} and {@code NANO_OF_DAY} The difference between each of the fields is calculated
 * independently from the others. At least one of the four fields must be present.
 * <p>
 * The four units are typically retained without normalization. However, years and months are normalized if
 * the range of months is fixed, as it is with ISO.
 * <p>
 * The result of this method can be a negative period if the end is before the start. The negative sign can
 * be different in each of the four major units.
 * 
 * @param start the start date, inclusive, not null
 * @param end the end date, exclusive, not null
 * @return the period between the date-times, not null
 * @throws DateTimeException if the two date-times do have similar available fields
 * @throws ArithmeticException if numeric overflow occurs
 */
public static Period between(DateTimeAccessor start, DateTimeAccessor end) {

  if (Chrono.from(start).equals(Chrono.from(end)) == false) {
    throw new DateTimeException("Unable to calculate period as date-times have different chronologies");
  }
  int years = 0;
  int months = 0;
  int days = 0;
  long nanos = 0;
  boolean valid = false;
  if (start.isSupported(YEAR)) {
    years = Jdk8Methods.safeToInt(Jdk8Methods.safeSubtract(end.getLong(YEAR), start.getLong(YEAR)));
    valid = true;
  }
  if (start.isSupported(MONTH_OF_YEAR)) {
    months = Jdk8Methods
        .safeToInt(Jdk8Methods.safeSubtract(end.getLong(MONTH_OF_YEAR), start.getLong(MONTH_OF_YEAR)));
    DateTimeValueRange startRange = Chrono.from(start).range(MONTH_OF_YEAR);
    DateTimeValueRange endRange = Chrono.from(end).range(MONTH_OF_YEAR);
    if (startRange.isFixed() && startRange.isIntValue() && startRange.equals(endRange)) {
      int monthCount = (int) (startRange.getMaximum() - startRange.getMinimum() + 1);
      long totMonths = ((long) months) + years * monthCount;
      months = (int) (totMonths % monthCount);
      years = Jdk8Methods.safeToInt(totMonths / monthCount);
    }
    valid = true;
  }
  if (start.isSupported(DAY_OF_MONTH)) {
    days = Jdk8Methods.safeToInt(Jdk8Methods.safeSubtract(end.getLong(DAY_OF_MONTH), start.getLong(DAY_OF_MONTH)));
    valid = true;
  }
  if (start.isSupported(NANO_OF_DAY)) {
    nanos = Jdk8Methods.safeSubtract(end.getLong(NANO_OF_DAY), start.getLong(NANO_OF_DAY));
    valid = true;
  }
  if (valid == false) {
    throw new DateTimeException("Unable to calculate period as date-times do not have any valid fields");
  }
  return create(years, months, days, nanos);
}
 
开发者ID:kiegroup,项目名称:optashift-employee-rostering,代码行数:64,代码来源:Period.java

示例4: convertToFraction

import java.time.calendrical.DateTimeValueRange; //导入方法依赖的package包/类
/**
 * 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 DateTimeField#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) {

  DateTimeValueRange range = this.field.range();
  if (range.isFixed() == false) {
    throw new DateTimeException("Unable to obtain fraction as field range is not fixed: " + this.field.getName());
  }
  range.checkValidValue(value, this.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();
}
 
开发者ID:m-m-m,项目名称:java8-backports,代码行数:30,代码来源:DateTimeFormatterBuilder.java

示例5: convertFromFraction

import java.time.calendrical.DateTimeValueRange; //导入方法依赖的package包/类
/**
 * Converts a fraction from 0 to 1 for this field to a value.
 * <p>
 * The fractional value must be between 0 (inclusive) and 1 (exclusive). It can only be returned if the
 * {@link DateTimeField#range() value range} is fixed. The value is obtained by calculation from the field
 * range 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 fractional second-of-minute of 0.25 would be converted to 15, assuming the standard
 * definition of 60 seconds in a minute.
 * 
 * @param fraction the fraction to convert, not null
 * @return the value of the field, valid for this rule
 * @throws DateTimeException if the value cannot be converted
 */
private long convertFromFraction(BigDecimal fraction) {

  DateTimeValueRange range = this.field.range();
  if (range.isFixed() == false) {
    throw new DateTimeException("Unable to obtain fraction as field range is not fixed: " + this.field.getName());
  }
  BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
  BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
  BigDecimal valueBD = fraction.multiply(rangeBD).setScale(0, RoundingMode.FLOOR).add(minBD);
  long value = valueBD.longValueExact();
  range.checkValidValue(value, this.field);
  return value;
}
 
开发者ID:m-m-m,项目名称:java8-backports,代码行数:29,代码来源:DateTimeFormatterBuilder.java


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