本文整理汇总了Java中java.time.calendrical.PeriodUnit.isDurationEstimated方法的典型用法代码示例。如果您正苦于以下问题:Java PeriodUnit.isDurationEstimated方法的具体用法?Java PeriodUnit.isDurationEstimated怎么用?Java PeriodUnit.isDurationEstimated使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.calendrical.PeriodUnit
的用法示例。
在下文中一共展示了PeriodUnit.isDurationEstimated方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: truncatedTo
import java.time.calendrical.PeriodUnit; //导入方法依赖的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>
* Not all units are accepted. The {@link ChronoUnit#DAYS days} unit and time units with an exact duration
* can be used, 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
*/
public LocalTime truncatedTo(PeriodUnit unit) {
if (unit == ChronoUnit.NANOS) {
return this;
} else if (unit == ChronoUnit.DAYS) {
return MIDNIGHT;
} else if (unit.isDurationEstimated()) {
throw new DateTimeException("Unit must not have an estimated duration");
}
long nod = toNanoOfDay();
long dur = unit.getDuration().toNanos();
if (dur >= NANOS_PER_DAY) {
throw new DateTimeException("Unit must not be a date unit");
}
nod = (nod / dur) * dur;
return ofNanoOfDay(nod);
}
示例2: plus
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
/**
* Returns a copy of this period with the specified period added.
* <p>
* The specified unit must be one of the supported units from {@link ChronoUnit}, {@code YEARS},
* {@code MONTHS} or {@code DAYS} or be a time unit with an {@link PeriodUnit#isDurationEstimated() exact
* duration}. Other units throw an exception.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amount the amount to add, positive or negative
* @param unit the unit that the amount is expressed in, not null
* @return a {@code Period} based on this period with the requested amount added, not null
* @throws ArithmeticException if numeric overflow occurs
*/
public Period plus(long amount, PeriodUnit unit) {
Jdk7Methods.Objects_requireNonNull(unit, "unit");
if (unit instanceof ChronoUnit) {
if (unit == YEARS || unit == MONTHS || unit == DAYS || unit.isDurationEstimated() == false) {
if (amount == 0) {
return this;
}
switch ((ChronoUnit) unit) {
case NANOS:
return plusNanos(amount);
case MICROS:
return plusNanos(Jdk8Methods.safeMultiply(amount, 1000L));
case MILLIS:
return plusNanos(Jdk8Methods.safeMultiply(amount, 1000000L));
case SECONDS:
return plusSeconds(amount);
case MINUTES:
return plusMinutes(amount);
case HOURS:
return plusHours(amount);
case HALF_DAYS:
return plusNanos(Jdk8Methods.safeMultiply(amount, 12 * NANOS_PER_HOUR));
case DAYS:
return plusDays(amount);
case MONTHS:
return plusMonths(amount);
case YEARS:
return plusYears(amount);
default :
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
}
}
if (unit.isDurationEstimated()) {
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return plusNanos(Duration.of(amount, unit).toNanos());
}
示例3: plus
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
/**
* Returns a copy of this duration with the specified duration added.
* <p>
* The duration amount is measured in terms of the specified unit. Only a subset of units are accepted by
* this method. The unit must either have an {@link PeriodUnit#isDurationEstimated() exact duration} or be
* {@link ChronoUnit#DAYS} which is treated as 24 hours. Other units throw an exception.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToAdd the amount of the period, measured in terms of the unit, positive or negative
* @param unit the unit that the period is measured in, must have an exact duration, not null
* @return a {@code Duration} based on this duration with the specified duration added, not null
* @throws ArithmeticException if numeric overflow occurs
*/
public Duration plus(long amountToAdd, PeriodUnit unit) {
Jdk7Methods.Objects_requireNonNull(unit, "unit");
if (unit == DAYS) {
return plus(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY), 0);
}
if (unit.isDurationEstimated()) {
throw new DateTimeException("Unit must not have an estimated duration");
}
if (amountToAdd == 0) {
return this;
}
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case NANOS:
return plusNanos(amountToAdd);
case MICROS:
return plusSeconds((amountToAdd / (1000000L * 1000)) * 1000).plusNanos(
(amountToAdd % (1000000L * 1000)) * 1000);
case MILLIS:
return plusMillis(amountToAdd);
case SECONDS:
return plusSeconds(amountToAdd);
}
return plusSeconds(Jdk8Methods.safeMultiply(unit.getDuration().seconds, amountToAdd));
}
Duration duration = unit.getDuration().multipliedBy(amountToAdd);
return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
}