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


Java TemporalAmount.getUnits方法代码示例

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


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

示例1: from

import java.time.temporal.TemporalAmount; //导入方法依赖的package包/类
/**
 * Obtains an instance of {@code Period} from a temporal amount.
 * <p>
 * This obtains a period based on the specified amount.
 * A {@code TemporalAmount} represents an  amount of time, which may be
 * date-based or time-based, which this factory extracts to a {@code Period}.
 * <p>
 * The conversion loops around the set of units from the amount and uses
 * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
 * and {@link ChronoUnit#DAYS DAYS} units to create a period.
 * If any other units are found then an exception is thrown.
 * <p>
 * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
 *
 * @param amount  the temporal amount to convert, not null
 * @return the equivalent period, not null
 * @throws DateTimeException if unable to convert to a {@code Period}
 * @throws ArithmeticException if the amount of years, months or days exceeds an int
 */
public static Period from(TemporalAmount amount) {
    if (amount instanceof Period) {
        return (Period) amount;
    }
    if (amount instanceof ChronoPeriod) {
        if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) {
            throw new DateTimeException("Period requires ISO chronology: " + amount);
        }
    }
    Objects.requireNonNull(amount, "amount");
    int years = 0;
    int months = 0;
    int days = 0;
    for (TemporalUnit unit : amount.getUnits()) {
        long unitAmount = amount.get(unit);
        if (unit == ChronoUnit.YEARS) {
            years = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.MONTHS) {
            months = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.DAYS) {
            days = Math.toIntExact(unitAmount);
        } else {
            throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit);
        }
    }
    return create(years, months, days);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:47,代码来源:Period.java

示例2: from

import java.time.temporal.TemporalAmount; //导入方法依赖的package包/类
/**
 * Obtains an instance of {@code Duration} from a temporal amount.
 * <p>
 * This obtains a duration based on the specified amount.
 * A {@code TemporalAmount} represents an  amount of time, which may be
 * date-based or time-based, which this factory extracts to a duration.
 * <p>
 * The conversion loops around the set of units from the amount and uses
 * the {@linkplain TemporalUnit#getDuration() duration} of the unit to
 * calculate the total {@code Duration}.
 * Only a subset of units are accepted by this method. The unit must either
 * have an {@linkplain TemporalUnit#isDurationEstimated() exact duration}
 * or be {@link ChronoUnit#DAYS} which is treated as 24 hours.
 * If any other units are found then an exception is thrown.
 *
 * @param amount  the temporal amount to convert, not null
 * @return the equivalent duration, not null
 * @throws DateTimeException if unable to convert to a {@code Duration}
 * @throws ArithmeticException if numeric overflow occurs
 */
public static Duration from(TemporalAmount amount) {
    Objects.requireNonNull(amount, "amount");
    Duration duration = ZERO;
    for (TemporalUnit unit : amount.getUnits()) {
        duration = duration.plus(amount.get(unit), unit);
    }
    return duration;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:29,代码来源:Duration.java


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