java.time.Duration.getSeconds()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(274)

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

Duration.getSeconds介绍

[英]Gets the number of seconds in this duration.

The length of the duration is stored using two fields - seconds and nanoseconds. The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to the length in seconds. The total duration is defined by calling this method and #getNano().

A Duration represents a directed distance between two points on the time-line. A negative duration is expressed by the negative sign of the seconds part. A duration of -1 nanosecond is stored as -1 seconds plus 999,999,999 nanoseconds.
[中]获取此持续时间内的秒数。
持续时间的长度使用两个字段存储-秒和纳秒。纳秒部分是一个从0到99999999的值,它是对以秒为单位的长度的调整。通过调用此方法和#getNano()定义总持续时间。
持续时间表示时间线上两点之间的定向距离。负持续时间由秒部分的负号表示。-1纳秒的持续时间存储为-1秒加9999999纳秒。

代码示例

代码示例来源:origin: thinkaurelius/titan

public static final int getTTLSeconds(Duration duration) {
  Preconditions.checkArgument(duration!=null && !duration.isZero(),"Must provide non-zero TTL");
  long ttlSeconds = Math.max(1,duration.getSeconds());
  assert ttlSeconds>0;
  Preconditions.checkArgument(ttlSeconds<=Integer.MAX_VALUE, "tll value is too large [%s] - value overflow",duration);
  return (int)ttlSeconds;
}

代码示例来源:origin: wildfly/wildfly

@Override
public void writeObject(ObjectOutput output, Duration duration) throws IOException {
  output.writeLong(duration.getSeconds());
  output.writeInt(duration.getNano());
}

代码示例来源:origin: azkaban/azkaban

public void start() {
 this.scheduler.scheduleAtFixedRate(() -> {
  FlowTriggerExecutionCleaner.this.flowTriggerInstanceLoader
    .deleteTriggerExecutionsFinishingOlderThan(System
      .currentTimeMillis() - RETENTION_PERIOD.toMillis());
 }, 0, CLEAN_INTERVAL.getSeconds(), TimeUnit.SECONDS);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public String toString() {
  StringBuilder sb = new StringBuilder();
  sb.append(getName()).append('=').append(getValue());
  if (StringUtils.hasText(getPath())) {
    sb.append("; Path=").append(getPath());
  }
  if (StringUtils.hasText(this.domain)) {
    sb.append("; Domain=").append(this.domain);
  }
  if (!this.maxAge.isNegative()) {
    sb.append("; Max-Age=").append(this.maxAge.getSeconds());
    sb.append("; Expires=");
    long millis = this.maxAge.getSeconds() > 0 ? System.currentTimeMillis() + this.maxAge.toMillis() : 0;
    sb.append(HttpHeaders.formatDate(millis));
  }
  if (this.secure) {
    sb.append("; Secure");
  }
  if (this.httpOnly) {
    sb.append("; HttpOnly");
  }
  if (StringUtils.hasText(this.sameSite)) {
    sb.append("; SameSite=").append(this.sameSite);
  }
  return sb.toString();
}

代码示例来源:origin: neo4j/neo4j

public static DurationValue duration( Duration value )
{
  requireNonNull( value, "Duration" );
  return newDuration( 0, 0, value.getSeconds(), value.getNano() );
}

代码示例来源:origin: hibernate/hibernate-orm

private long getLockWaitTimeoutInSeconds(int timeoutInMilliseconds) {
  Duration duration = Duration.ofMillis( timeoutInMilliseconds );
  long timeoutInSeconds = duration.getSeconds();
  if ( duration.getNano() != 0 ) {
    LOG.info( "Changing the query timeout from " + timeoutInMilliseconds + " ms to " + timeoutInSeconds
        + " s, because HANA requires the timeout in seconds" );
  }
  return timeoutInSeconds;
}

代码示例来源:origin: neo4j/neo4j

private Duration nextDurationRaw()
{
  return Duration.ofSeconds( nextLong( DAYS.getDuration().getSeconds() ), nextLong( NANOS_PER_SECOND ) );
}

代码示例来源:origin: thinkaurelius/titan

@Override
  public void write(WriteBuffer buffer, Duration attribute) {
    secondsSerializer.write(buffer,attribute.getSeconds());
    nanosSerializer.write(buffer,attribute.getNano());
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void applyCookies() {
  for (String name : getCookies().keySet()) {
    for (ResponseCookie httpCookie : getCookies().get(name)) {
      Cookie cookie = new Cookie(name, httpCookie.getValue());
      if (!httpCookie.getMaxAge().isNegative()) {
        cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds());
      }
      if (httpCookie.getDomain() != null) {
        cookie.setDomain(httpCookie.getDomain());
      }
      if (httpCookie.getPath() != null) {
        cookie.setPath(httpCookie.getPath());
      }
      cookie.setSecure(httpCookie.isSecure());
      cookie.setHttpOnly(httpCookie.isHttpOnly());
      this.response.addCookie(cookie);
    }
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

public static String elapsedTime(Date d1, Date d2){
 try{
  Duration period = Duration.between(d1.toInstant(), d2.toInstant());
  // Note: this will become easier with Java 9, using toDaysPart() etc.
  long days = period.toDays();
  period = period.minusDays(days);
  long hours = period.toHours();
  period = period.minusHours(hours);
  long minutes = period.toMinutes();
  period = period.minusMinutes(minutes);
  long seconds = period.getSeconds();
  return days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
 } catch(java.lang.IllegalArgumentException e) {
  log.warn(e);
 }
 return "";
}

代码示例来源:origin: neo4j/neo4j

@Test
void shouldConvertNanosOfDayToUTC()
{
  int nanosOfDayLocal = 42;
  Duration offsetDuration = Duration.ofMinutes( 35 );
  long nanosOfDayUTC = TemporalUtil.nanosOfDayToUTC( nanosOfDayLocal, (int) offsetDuration.getSeconds() );
  assertEquals( nanosOfDayLocal - offsetDuration.toNanos(), nanosOfDayUTC );
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testCircuitBreakerOnIgnoredErrorEvent() {
  CircuitBreakerOnIgnoredErrorEvent circuitBreakerEvent = new CircuitBreakerOnIgnoredErrorEvent("test",
      Duration.ofSeconds(1), new IOException());
  assertThat(circuitBreakerEvent.getCircuitBreakerName()).isEqualTo("test");
  assertThat(circuitBreakerEvent.getElapsedDuration().getSeconds()).isEqualTo(1);
  assertThat(circuitBreakerEvent.getThrowable()).isInstanceOf(IOException.class);
  assertThat(circuitBreakerEvent.getEventType()).isEqualTo(Type.IGNORED_ERROR);
  assertThat(circuitBreakerEvent.toString()).contains("CircuitBreaker 'test' recorded an error which has been ignored: 'java.io.IOException'.");
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testCircuitBreakerOnErrorEvent() {
  CircuitBreakerOnErrorEvent circuitBreakerEvent = new CircuitBreakerOnErrorEvent("test",
      Duration.ofSeconds(1), new IOException());
  assertThat(circuitBreakerEvent.getCircuitBreakerName()).isEqualTo("test");
  assertThat(circuitBreakerEvent.getElapsedDuration().getSeconds()).isEqualTo(1);
  assertThat(circuitBreakerEvent.getThrowable()).isInstanceOf(IOException.class);
  assertThat(circuitBreakerEvent.getEventType()).isEqualTo(Type.ERROR);
  assertThat(circuitBreakerEvent.toString()).contains("CircuitBreaker 'test' recorded an error: 'java.io.IOException'.");
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testCircuitBreakerOnSuccessEvent() {
  CircuitBreakerOnSuccessEvent circuitBreakerEvent = new CircuitBreakerOnSuccessEvent("test", Duration.ofSeconds(1));
  assertThat(circuitBreakerEvent.getCircuitBreakerName()).isEqualTo("test");
  assertThat(circuitBreakerEvent.getElapsedDuration().getSeconds()).isEqualTo(1);
  assertThat(circuitBreakerEvent.getEventType()).isEqualTo(Type.SUCCESS);
  assertThat(circuitBreakerEvent.toString()).contains("CircuitBreaker 'test' recorded a successful call.");
}

代码示例来源:origin: prestodb/presto

@Override
public void serialize(Duration duration, JsonGenerator generator, SerializerProvider provider) throws IOException
{
  if (useTimestamp(provider)) {
    if (useNanoseconds(provider)) {
      generator.writeNumber(DecimalUtils.toBigDecimal(
          duration.getSeconds(), duration.getNano()
      ));
    } else {
      generator.writeNumber(duration.toMillis());
    }
  } else {
    // Does not look like we can make any use of DateTimeFormatter here?
    generator.writeString(duration.toString());
  }
}

代码示例来源:origin: resilience4j/resilience4j

@Test()
public void shouldSetWaitInterval() {
  CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().waitDurationInOpenState(Duration.ofSeconds(1)).build();
  then(circuitBreakerConfig.getWaitDurationInOpenState().getSeconds()).isEqualTo(1);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void maxAge() {
  Duration maxAge = Duration.ofDays(365);
  String expires = HttpHeaders.formatDate(System.currentTimeMillis() + maxAge.toMillis());
  expires = expires.substring(0, expires.indexOf(":") + 1);
  assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge).build().toString(), allOf(
      startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires),
      endsWith(" GMT")));
  assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge.getSeconds()).build().toString(), allOf(
      startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires),
      endsWith(" GMT")));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void applyCookies() {
  for (String name : getCookies().keySet()) {
    for (ResponseCookie httpCookie : getCookies().get(name)) {
      Cookie cookie = new CookieImpl(name, httpCookie.getValue());
      if (!httpCookie.getMaxAge().isNegative()) {
        cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds());
      }
      if (httpCookie.getDomain() != null) {
        cookie.setDomain(httpCookie.getDomain());
      }
      if (httpCookie.getPath() != null) {
        cookie.setPath(httpCookie.getPath());
      }
      cookie.setSecure(httpCookie.isSecure());
      cookie.setHttpOnly(httpCookie.isHttpOnly());
      this.exchange.getResponseCookies().putIfAbsent(name, cookie);
    }
  }
}

代码示例来源:origin: resilience4j/resilience4j

@Test()
public void shouldSetDefaultSettings() {
  CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.ofDefaults();
  then(circuitBreakerConfig.getFailureRateThreshold()).isEqualTo(CircuitBreakerConfig.DEFAULT_MAX_FAILURE_THRESHOLD);
  then(circuitBreakerConfig.getRingBufferSizeInHalfOpenState()).isEqualTo(CircuitBreakerConfig.DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE);
  then(circuitBreakerConfig.getRingBufferSizeInClosedState()).isEqualTo(CircuitBreakerConfig.DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE);
  then(circuitBreakerConfig.getWaitDurationInOpenState().getSeconds()).isEqualTo(CircuitBreakerConfig.DEFAULT_WAIT_DURATION_IN_OPEN_STATE);
  then(circuitBreakerConfig.getRecordFailurePredicate()).isNotNull();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void applyCookies() {
  for (String name : getCookies().keySet()) {
    for (ResponseCookie httpCookie : getCookies().get(name)) {
      Cookie cookie = new DefaultCookie(name, httpCookie.getValue());
      if (!httpCookie.getMaxAge().isNegative()) {
        cookie.setMaxAge(httpCookie.getMaxAge().getSeconds());
      }
      if (httpCookie.getDomain() != null) {
        cookie.setDomain(httpCookie.getDomain());
      }
      if (httpCookie.getPath() != null) {
        cookie.setPath(httpCookie.getPath());
      }
      cookie.setSecure(httpCookie.isSecure());
      cookie.setHttpOnly(httpCookie.isHttpOnly());
      this.response.addCookie(cookie);
    }
  }
}

相关文章