Java Code Examples for java.time.Duration#toString()

The following examples show how to use java.time.Duration#toString() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public Optional<String> execute(String expression)
{
    Matcher expressionMatcher = DIFF_DATE_PATTERN.matcher(expression);
    if (expressionMatcher.find())
    {
        ZonedDateTime firstZonedDateTime = getZonedDateTime(expressionMatcher, FIRST_INPUT_DATE_GROUP,
                FIRST_INPUT_FORMAT_GROUP);
        ZonedDateTime secondZonedDateTime = getZonedDateTime(expressionMatcher, SECOND_INPUT_DATE_GROUP,
                SECOND_INPUT_FORMAT_GROUP);
        Duration duration = Duration.between(firstZonedDateTime, secondZonedDateTime);
        String durationAsString = duration.toString();
        return Optional.ofNullable(expressionMatcher.group(FORMAT_GROUP))
                       .map(String::trim)
                       .map(String::toUpperCase)
                       .map(t -> EnumUtils.getEnum(ChronoUnit.class, t))
                       .map(u -> u.between(firstZonedDateTime, secondZonedDateTime))
                       .map(Object::toString)
                       .or(() -> processNegative(duration, durationAsString));
    }
    return Optional.empty();
}
 
Example 2
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(Duration duration, Class<X> type, WrapperOptions options) {
	if ( duration == null ) {
		return null;
	}

	if ( Duration.class.isAssignableFrom( type ) ) {
		return (X) duration;
	}

	if ( String.class.isAssignableFrom( type ) ) {
		return (X) duration.toString();
	}

	if ( Long.class.isAssignableFrom( type ) ) {
		return (X) Long.valueOf( duration.toNanos() );
	}

	throw unknownUnwrap( type );
}
 
Example 3
private void checkIdleTimeout()
{
    if ( wdStatus == HolderStatus.ACTIVE )
    {
        final Duration idleDuration = Duration.between( lastActivity, Instant.now() );
        if ( idleDuration.toMillis() > settings.getIdleTimeoutMS() )
        {
            final String msg = "ldap idle timeout detected ("
                    + idleDuration.toString()
                    + "), closing connection id="
                    + watchdogWrapper.getIdentifier();

            disconnectRealProvider( msg );
        }
    }
}
 
Example 4
private void checkMaxLifetimeDuration()
{
    if ( wdStatus == HolderStatus.ACTIVE )
    {
        final Duration maxConnectionLifetime = settings.getMaxConnectionLifetime();
        if ( !allowDisconnect || maxConnectionLifetime == null )
        {
            return;
        }

        final Duration ageOfConnection = Duration.between( connectionEstablishedTime, Instant.now() );
        if ( ageOfConnection.compareTo( maxConnectionLifetime ) > 0 )
        {
            final String msg = "connection lifetime (" + ageOfConnection.toString()
                    + ") exceeded maximum configured lifetime (" + maxConnectionLifetime.toString() + ")";

            disconnectRealProvider( msg );
        }
    }
}
 
Example 5
protected static String durationFormat(Duration duration) {
    String durationStr = duration.toString();
    if (durationStr.contains("S")) {
        return duration.toMillis() / 1000 + "s";
    } else if (!durationStr.contains("S") && durationStr.contains("M")) {
        return duration.toMinutes() + "m";
    } else if (!durationStr.contains("S") && !durationStr.contains("M")) {
        if (duration.toHours() % 24 == 0) {
            return duration.toDays() + "d";
        } else {
            return duration.toHours() + "h";
        }
    }
    throw new UnsupportedOperationException("不支持的时间周期:" + duration.toString());
}
 
Example 6
Source Project: dbeam   File: BeamHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static PipelineResult waitUntilDone(
    final PipelineResult result, final Duration exportTimeout) {
  // terminal state might be null, such as:
  // {{ @link org.apache.beam.runners.dataflow.DataflowPipelineJob.waitUntilFinish }}
  @Nullable
  final PipelineResult.State terminalState =
      result.waitUntilFinish(org.joda.time.Duration.millis(exportTimeout.toMillis()));
  if (terminalState == null || !terminalState.isTerminal()) {
    try {
      result.cancel();
    } catch (IOException e) {
      throw new Pipeline.PipelineExecutionException(
          new Exception(
              String.format(
                  "Job exceeded timeout of %s, but was not possible to cancel, "
                      + "finished with terminalState %s",
                  exportTimeout.toString(), terminalState),
              e));
    }
    throw new Pipeline.PipelineExecutionException(
        new Exception("Job cancelled after exceeding timeout " + exportTimeout.toString()));
  }
  if (!terminalState.equals(PipelineResult.State.DONE)) {
    throw new Pipeline.PipelineExecutionException(
        new Exception("Job finished with terminalState " + terminalState.toString()));
  }
  return result;
}
 
Example 7
Source Project: fastbreak   File: CircuitBreakerImpl.java    License: Apache License 2.0 5 votes vote down vote up
protected CircuitBreakerTimeoutException generateNewCircuitBreakerTimeoutException(long timeoutNanos) {
    Duration duration = Duration.ofNanos(timeoutNanos);
    return new CircuitBreakerTimeoutException(
        id,
        "The CompletableFuture took longer than the CircuitBreaker timeout of " + duration.toString()
        + ". circuit_breaker_id=" + id
    );
}
 
Example 8
private void checkOperationTimeout()
{
    if ( wdStatus == HolderStatus.ACTIVE )
    {
        final Duration operationDuration = Duration.between( lastActivity, Instant.now() );
        if ( operationDuration.toMillis() > settings.getOperationTimeoutMS() )
        {
            final String msg = "ldap operation timeout detected ("
                    + operationDuration.toString()
                    + "), closing questionable connection id="
                    + watchdogWrapper.getIdentifier();
            disconnectRealProvider( msg );
        }
    }
}
 
Example 9
Source Project: super-csv   File: FmtDuration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws SuperCsvCellProcessorException if value is null or not a Duration
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	if( !(value instanceof Duration) ) {
		throw new SuperCsvCellProcessorException(Duration.class, value, context, this);
	}
	final Duration duration = (Duration) value;
	final String result = duration.toString();
	return next.execute(result, context);
}
 
Example 10
@Override
public String convert(Duration duration) {
    String result = null;

    if (log.isTraceEnabled()) {
        log.trace("Converting Duration {} to string", duration.toString());
    }

    result = duration.toString();

    return result;
}
 
Example 11
Source Project: spring-analysis-note   File: DurationFormatter.java    License: MIT License 4 votes vote down vote up
@Override
public String print(Duration object, Locale locale) {
	return object.toString();
}
 
Example 12
@Override
public String toNonNullString(Duration value) {
    return value.toString();
}
 
Example 13
Source Project: cucumber-performance   File: CucumberPerf.java    License: MIT License 4 votes vote down vote up
/**
 * @return Duration of of Cucumber Perf execution.
 */
public String getRunTime() {
	Duration d = Duration.between(start, end);
	return d.toString();
}
 
Example 14
@Override
public String print(Duration object, Locale locale) {
	return object.toString();
}
 
Example 15
Source Project: jadira   File: LongColumnDurationMapper.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public String toNonNullString(Duration value) {
    return value.toString();
}
 
Example 16
Source Project: emodb   File: ZkDurationSerializer.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public String toString(Duration duration) {
    return duration.toString();
}
 
Example 17
@Override
public String toNonNullString(Duration value) {
    return value.toString();
}
 
Example 18
@Override
public String marshal(Duration value) {
    return value != null ? value.toString() : null;
}
 
Example 19
Source Project: javers   File: DurationTypeAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public String serialize(Duration sourceValue) {
    return sourceValue.toString();
}
 
Example 20
@Override
public String toNonNullString(Duration value) {
    return value.toString();
}