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


Java DateTimeValueRange.equals方法代码示例

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


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

示例1: 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


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