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


Java TemporalUnit.getDuration方法代码示例

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


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

示例1: truncatedTo

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:39,代码来源:Instant.java

示例2: truncatedTo

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncating the time returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:36,代码来源:LocalTime.java

示例3: truncatedTo

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncation returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:36,代码来源:LocalTime.java

示例4: truncatedTo

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = Math.floorDiv(nod, dur) * dur ;
    return plusNanos(result - nod);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:Instant.java

示例5: truncatedTo

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Returns a copy of this {@code Duration} truncated to the specified unit.
 * <p>
 * Truncating the duration returns a copy of the original with conceptual fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other ChronoUnits throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit the unit to truncate to, not null
 * @return a {@code Duration} based on this duration with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @since 9
 */
public Duration truncatedTo(TemporalUnit unit) {
    Objects.requireNonNull(unit, "unit");
    if (unit == ChronoUnit.SECONDS && (seconds >= 0 || nanos == 0)) {
        return new Duration(seconds, 0);
    } else if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur ;
    return plusNanos(result - nod);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:41,代码来源:Duration.java


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