java.time.LocalTime.until()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(207)

本文整理了Java中java.time.LocalTime.until()方法的一些代码示例,展示了LocalTime.until()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LocalTime.until()方法的具体详情如下:
包路径:java.time.LocalTime
类名称:LocalTime
方法名:until

LocalTime.until介绍

[英]Calculates the period between this time and another time in terms of the specified unit.

This calculates the period between two times in terms of a single unit. The start and end points are this and the specified time. The result will be negative if the end is before the start. The Temporal passed to this method must be a LocalTime. For example, the period in hours between two times can be calculated using startTime.until(endTime, HOURS).

The calculation returns a whole number, representing the number of complete units between the two times. For example, the period in hours between 11:30 and 13:29 will only be one hour as it is one minute short of two hours.

This method operates in association with TemporalUnit#between. The result of this method is a long representing the amount of the specified unit. By contrast, the result of between is an object that can be used directly in addition/subtraction:

long period = start.until(end, HOURS);   // this method 
dateTime.plus(HOURS.between(start, end));      // use in plus/minus

The calculation is implemented in this method for ChronoUnit. The units NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS and HALF_DAYS are supported. Other ChronoUnit values will throw an exception.

If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.between(Temporal, Temporal)passing this as the first argument and the input temporal as the second argument.

This instance is immutable and unaffected by this method call.
[中]根据指定的单位计算此时间与另一时间之间的时间段。
这将以单个单位计算两次之间的周期。开始点和结束点是此时间点和指定时间点。如果结束早于开始,结果将为负值。传递给此方法的时间必须是LocalTime。例如,可以使用startTime计算两次之间的时间段(以小时为单位)。直到(结束时间,小时)。
计算返回一个整数,表示两次计算之间的完整单位数。例如,11:30到13:29之间的时间段(以小时为单位)将仅为一小时,因为它比两小时短一分钟。
此方法与TemporalUnit#between结合使用。此方法的结果是表示指定单位数量的长字符串。相比之下,between的结果是可以直接用于加法/减法的对象:

long period = start.until(end, HOURS);   // this method 
dateTime.plus(HOURS.between(start, end));      // use in plus/minus

计算是在这种方法中实现的。支持单位纳秒、微秒、毫秒、分钟、小时和半天。其他ChronoUnit值将引发异常。
如果该单元不是ChronoUnit,则通过调用TemporalUnit获得该方法的结果。(时态,时态)将其作为第一个参数传递,将输入时态作为第二个参数传递。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: eXparity/hamcrest-date

@Override
public long difference(final LocalTime other, final ChronoUnit unit) {
  return Math.abs(wrapped.until(other, unit));
}

代码示例来源:origin: stackoverflow.com

import java.time.LocalTime;
import static java.time.temporal.ChronoUnit.MINUTES;

public class SO {
  public static void main(String[] args) {
    LocalTime l1 = LocalTime.parse("02:53:40");
    LocalTime l2 = LocalTime.parse("02:54:27");
    System.out.println(l1.until(l2, MINUTES));
    System.out.println(MINUTES.between(l1, l2));
  }
}

代码示例来源:origin: com.github.seratch/java-time-backport

@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
  @SuppressWarnings("unchecked")
  ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) toLocalDate().getChronology().localDateTime(endExclusive);
  if (unit instanceof ChronoUnit) {
    ChronoUnit f = (ChronoUnit) unit;
    if (f.isTimeBased()) {
      long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY);
      switch (f) {
        case NANOS: amount = Jdk8Methods.safeMultiply(amount, NANOS_PER_DAY); break;
        case MICROS: amount = Jdk8Methods.safeMultiply(amount, MICROS_PER_DAY); break;
        case MILLIS: amount = Jdk8Methods.safeMultiply(amount, MILLIS_PER_DAY); break;
        case SECONDS: amount = Jdk8Methods.safeMultiply(amount, SECONDS_PER_DAY); break;
        case MINUTES: amount = Jdk8Methods.safeMultiply(amount, MINUTES_PER_DAY); break;
        case HOURS: amount = Jdk8Methods.safeMultiply(amount, HOURS_PER_DAY); break;
        case HALF_DAYS: amount = Jdk8Methods.safeMultiply(amount, 2); break;
      }
      return Jdk8Methods.safeAdd(amount, time.until(end.toLocalTime(), unit));
    }
    ChronoLocalDate endDate = end.toLocalDate();
    if (end.toLocalTime().isBefore(time)) {
      endDate = endDate.minus(1, ChronoUnit.DAYS);
    }
    return date.until(endDate, unit);
  }
  return unit.between(this, end);
}

相关文章