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

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

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

LocalTime.plus介绍

[英]Returns a copy of this time with the specified period added.

This method returns a new time based on this time with the specified period added. This can be used to add any period that is defined by a unit, for example to add hours, minutes or seconds. The unit is responsible for the details of the calculation, including the resolution of any edge cases in the calculation.

This instance is immutable and unaffected by this method call.
[中]返回添加了指定时间段的此时间的副本。
此方法返回基于此时间并添加指定时段的新时间。这可用于添加由单位定义的任何时段,例如添加小时、分钟或秒。该单位负责计算的细节,包括计算中任何边缘情况的解决方案。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: lettuce-io/lettuce-core

static String formatTimeout(Duration duration) {
  if (duration.isZero()) {
    return "no timeout";
  }
  LocalTime time = LocalTime.MIDNIGHT.plus(duration);
  if (isExactMinutes(duration)) {
    return MINUTES.format(time);
  }
  if (isExactSeconds(duration)) {
    return SECONDS.format(time);
  }
  if (isExactMillis(duration)) {
    return MILLISECONDS.format(time);
  }
  return String.format("%d ns", duration.toNanos());
}

代码示例来源:origin: org.postgresql/postgresql

public synchronized String toString(LocalTime localTime) {
 sbuf.setLength(0);
 if (localTime.isAfter(MAX_TIME)) {
  return "24:00:00";
 }
 int nano = localTime.getNano();
 if (nanosExceed499(nano)) {
  // Technically speaking this is not a proper rounding, however
  // it relies on the fact that appendTime just truncates 000..999 nanosecond part
  localTime = localTime.plus(ONE_MICROSECOND);
 }
 appendTime(sbuf, localTime);
 return sbuf.toString();
}

代码示例来源:origin: MorphiaOrg/morphia

@Test
public void rangeQueries() {
  Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
  LocalDate localDate = LocalDate.of(1995, 10, 15);
  LocalDateTime localDateTime = LocalDateTime.of(2016, 4, 10, 14, 15, 16, 123 * 1000000);
  LocalTime localTime = LocalTime.of(10, 29, 15, 848493);
  for (int i = 0; i < 10; i++) {
    createEntity(getDs(),
           instant.plus(i, DAYS),
           localDate.plus(i, DAYS),
           localDateTime.plus(i, DAYS),
           localTime.plus(i, ChronoUnit.HOURS));
  }
  Assert.assertEquals(2L, getDs().find(Java8Entity.class).field("instant").lessThanOrEq(instant.plus(1, DAYS)).count());
  Assert.assertEquals(1L, getDs().find(Java8Entity.class).field("localDate").equal(localDate.plus(1, DAYS)).count());
  Assert.assertEquals(0L, getDs().find(Java8Entity.class).field("localDate").equal(localDate.minus(1, DAYS)).count());
  Assert.assertEquals(9L, getDs().find(Java8Entity.class).field("localDateTime")
                  .notEqual(localDateTime.plus(6, DAYS)).count());
}

代码示例来源:origin: com.github.almex/raildelays-api

/**
 * Translate a {@link TimeDelay} into a {@link LocalTime} in order to get the effective time of this
 * {@link TimeDelay}.
 *
 * @return a non-null {@link LocalTime} which is a combination of {@code expectedTime}
 * plus its {@code delay}.
 * @throws DateTimeException if the addition cannot be made
 * @throws ArithmeticException if numeric overflow occurs
 */
public LocalTime getEffectiveTime() {
  return expectedTime.plus(delay, DELAY_UNIT);
}

代码示例来源:origin: lesfurets/dOOv

@Override
Function<LocalTime, LocalTime> plusFunction(int value, TemporalUnit unit) {
  return time -> time.plus(value, unit);
}

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

/**
 * Returns a copy of this time with the specified period subtracted.
 * <p>
 * This method returns a new time based on this time with the specified period subtracted.
 * This can be used to subtract any period that is defined by a unit, for example to subtract hours, minutes or seconds.
 * The unit is responsible for the details of the calculation, including the resolution
 * of any edge cases in the calculation.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
 * @param unit  the unit of the period to subtract, not null
 * @return a {@code LocalTime} based on this time with the specified period subtracted, not null
 * @throws DateTimeException if the unit cannot be added to this type
 */
@Override
public LocalTime minus(long amountToSubtract, TemporalUnit unit) {
  return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
}

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

import java.time.Duration;
import java.time.LocalTime;

public class TimeDiff {
  public static void main(String[] args) {
    LocalTime start = LocalTime.parse("05:53");
    LocalTime end = LocalTime.parse("17:57");
    // find the duration between the start and end times
    Duration duration = Duration.between(start, end);
    // add half the duration to the start time to find the midpoint
    LocalTime midPoint = start.plus(duration.dividedBy(2));
    System.out.println(midPoint);
  }
}

代码示例来源:origin: zavtech/morpheus-core

@Override
public boolean hasNext() {
  if (excludes != null) {
    while (excludes.test(value) && inBounds(value)) {
      value = ascend ? value.plus(step) : value.minus(step);
    }
  }
  return inBounds(value);
}
@Override

代码示例来源:origin: zavtech/morpheus-core

@Override
  public LocalTime next() {
    final LocalTime next = value;
    value = ascend ? value.plus(step) : value.minus(step);
    return next;
  }
};

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

String inputStart = "07:00";
String inputStop = "14:00";

LocalTime start = LocalTime.parse ( inputStart );
LocalTime stop = LocalTime.parse ( inputStop );
LocalTime nextTime = start;

Duration duration = Duration.ofMinutes ( 5 );

long initialCapacity = ( Duration.between ( start , stop ).toMinutes () / duration.toMinutes () ) + 1;  // Optional line of code.  Could be omitted.
List<LocalTime> times = new ArrayList ( ( int ) initialCapacity );
while (  ! nextTime.isAfter ( stop ) ) {
  times.add ( nextTime );
  nextTime = nextTime.plus ( duration );  //  Or call .plusMinutes( int ) and pass a number of minutes.
}

代码示例来源:origin: org.bytemechanics/test-drive-core

/**
   * Format duration as string (null protected)
   * @param _duration exception to generate stacktrace
   * @param _pattern pattern to use if null uses ISO_TIME
   * @return duration in format provided or string with an error if failed
   */
  public static final String toString(final Duration _duration,final String _pattern){

    try{
      if(_duration==null)
        return "null";
      final DateTimeFormatter formatter=Optional.ofNullable(_pattern)
                        .map(DateTimeFormatter::ofPattern)
                        .orElse(DateTimeFormatter.ISO_TIME);
      return formatter.format(LocalTime.MIDNIGHT.plus(_duration));
    }catch(Exception e){
      return "<unable to print stacktrace: "+String.valueOf(e.getMessage())+">";
    }
  }
}

代码示例来源:origin: com.guestful.module/guestful.module.jsr310-extensions

public static LocalTime plus(LocalTime time, LocalTime add) {
  return time.plus(toDuration(add));
}

代码示例来源:origin: io.rhiot/rhiot-cloudplatform-service-device

@Override
public List<String> disconnected() {
  return list().stream().filter(device -> {
    LocalTime updated = ofInstant(ofEpochMilli(device.getLastUpdate().getTime()), ZoneId.systemDefault()).toLocalTime();
    return updated.plus(disconnectionPeriod, ChronoUnit.MILLIS).isBefore(LocalTime.now());
  }).map(Device::getDeviceId).collect(toList());
}

代码示例来源:origin: io.lettuce/lettuce-core

static String formatTimeout(Duration duration) {
  if (duration.isZero()) {
    return "no timeout";
  }
  LocalTime time = LocalTime.MIDNIGHT.plus(duration);
  if (isExactMinutes(duration)) {
    return MINUTES.format(time);
  }
  if (isExactSeconds(duration)) {
    return SECONDS.format(time);
  }
  if (isExactMillis(duration)) {
    return MILLISECONDS.format(time);
  }
  return String.format("%d ns", duration.toNanos());
}

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

if(lastEnd.plus(1, ChronoUnit.MINUTES).isBefore(start)){
  System.err.println("Missing coverage between: " + lastEnd + " and " + start);
  return false;

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

/**
 * Returns a copy of this time with the specified period added.
 * <p>
 * This method returns a new time based on this time with the specified period added.
 * This can be used to add any period that is defined by a unit, for example to add hours, minutes or seconds.
 * The unit is responsible for the details of the calculation, including the resolution
 * of any edge cases in the calculation.
 * The offset is not part of the calculation and will be unchanged in the result.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param amountToAdd  the amount of the unit to add to the result, may be negative
 * @param unit  the unit of the period to add, not null
 * @return an {@code OffsetTime} based on this time with the specified period added, not null
 * @throws DateTimeException if the unit cannot be added to this type
 */
@Override
public OffsetTime plus(long amountToAdd, TemporalUnit unit) {
  if (unit instanceof ChronoUnit) {
    return with(time.plus(amountToAdd, unit), offset);
  }
  return unit.addTo(this, amountToAdd);
}

代码示例来源:origin: zavtech/morpheus-core

@Override
public long estimateSize() {
  if (ascend) {
    final long stepSize = ChronoUnit.MILLIS.between(start(), start().plus(step));
    final long totalSize = ChronoUnit.MILLIS.between(start(), end());
    return (long)Math.ceil((double)totalSize / (double)stepSize);
  } else {
    final long stepSize = ChronoUnit.MILLIS.between(start(), start().plus(step));
    final long totalSize = ChronoUnit.MILLIS.between(end(), start());
    return (long)Math.ceil((double)totalSize / (double)stepSize);
  }
}

代码示例来源:origin: dremio/dremio-oss

protected AccelerationData.Info newInfo(final Materialization materialization) {
 final com.dremio.service.accelerator.proto.JobDetails details = materialization.getJob();
 final Long jobStart = details.getJobStart();
 final Long jobEnd = details.getJobEnd();
 final AccelerationData.Info info = new AccelerationData.Info();
 if (jobStart != null) {
  info.setStart(DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(jobStart), ZoneOffset.UTC)));
 }
 if (jobEnd != null) {
  info.setEnd(DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(jobEnd), ZoneOffset.UTC)));
 }
 if (jobStart != null && jobEnd != null) {
  final Duration duration = Duration.ofMillis(jobEnd - jobStart);
  info.setDuration(DateTimeFormatter.ISO_LOCAL_TIME.format(LocalTime.MIDNIGHT.plus(duration)));
 }
 info.setJobId(details.getJobId())
  .setInputBytes(details.getInputBytes())
  .setInputRecords(details.getInputRecords())
  .setOutputBytes(details.getOutputBytes())
  .setOutputRecords(details.getOutputRecords());
 return info;
}

代码示例来源:origin: zavtech/morpheus-core

@Override
public List<Range<LocalTime>> split(int splitThreshold) {
  final int[] segmentSteps = getSegmentSteps((int)estimateSize());
  if (segmentSteps[0] < splitThreshold) {
    return Collections.singletonList(this);
  } else {
    final Duration stepSize = step;
    final List<Range<LocalTime>> segments = new ArrayList<>();
    for (int i=0; i<segmentSteps.length; ++i) {
      final Duration segmentSize = stepSize.multipliedBy(segmentSteps[i]);
      if (i == 0) {
        final LocalTime end = isAscending() ? start().plus(segmentSize) : start().minus(segmentSize);
        final Range<LocalTime> range = Range.of(start(), end, step, excludes);
        segments.add(range);
      } else {
        final Range<LocalTime> previous = segments.get(i-1);
        final LocalTime end = isAscending() ? previous.end().plus(segmentSize) : previous.end().minus(segmentSize);
        final Range<LocalTime> next = Range.of(previous.end(), end, step, excludes);
        segments.add(next);
      }
    }
    return segments;
  }
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

@Test
public void timeAttribute() throws Exception {
  testSingleAndSet(AttributeAccess.ofTime(), AttributeAccess.ofTimeSet(),
      asSet(LocalTime.now(), LocalTime.now().plus(3, ChronoUnit.MINUTES)),
      TimeAttributeType.of(),
      AttributeDefinitionBuilder.of("time-attribute", LABEL, TimeAttributeType.of()).build());
}

相关文章