Java Code Examples for java.util.concurrent.TimeUnit.toString()

The following are Jave code examples for showing how to use toString() of the java.util.concurrent.TimeUnit class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Biliomi   File: TimeFormatter.java   Source Code and License Vote up 6 votes
/**
 * Format a duration (Quantity of time) to human readable string
 *
 * @param duration   The period to format
 * @param targetUnit The target unit
 * @return A string formatted as [amount] [i18n scalar descriptor]
 */
public String timeQuantity(Duration duration, TimeUnit targetUnit) {
  switch (targetUnit) {
    case DAYS:
      long days = duration.getStandardDays();
      return days + " " + i18n.getIfElse(MathUtils.isPlural(days), TIMEUNIT_DAYS, TIMEUNIT_DAY);
    case HOURS:
      long hours = duration.getStandardHours();
      return hours + " " + i18n.getIfElse(MathUtils.isPlural(hours), TIMEUNIT_HOURS, TIMEUNIT_HOUR);
    case MINUTES:
      long minutes = duration.getStandardMinutes();
      return minutes + " " + i18n.getIfElse(MathUtils.isPlural(minutes), TIMEUNIT_MINUTES, TIMEUNIT_MINUTE);
    case SECONDS:
      long seconds = duration.getStandardSeconds();
      return seconds + " " + i18n.getIfElse(MathUtils.isPlural(seconds), TIMEUNIT_SECONDS, TIMEUNIT_SECOND);
    default:
      throw new UnsupportedOperationException("Quantifying " + targetUnit.toString() + " is not supported");
  }
}
 
Example 2
Project: aws-photosharing-example   File: TokenGenerator.java   Source Code and License Vote up 5 votes
public boolean validateToken(String token, Integer loginTime, TimeUnit timeUnit) {
    try {
        String key = jasypt.decrypt(token);

        String [] keyParts = key.split("\\|");
        String strDate = keyParts[keyParts.length - 1];
        Date date = format.parse(strDate);

        Date currentTime = new Date();

        long duration = currentTime.getTime() - date.getTime();
        long diff;

        if (timeUnit.equals(TimeUnit.MINUTES))
            diff = TimeUnit.MILLISECONDS.toMinutes(duration);
        else if (timeUnit.equals(TimeUnit.SECONDS))
            diff = TimeUnit.MILLISECONDS.toSeconds(duration);
        else
            throw new UnsupportedOperationException(timeUnit.toString() + " not supported");

        if (diff > loginTime)
            return false;

        return true;
    }

    catch (Exception exc) {
        exc.printStackTrace();

        return false;
    }
}
 
Example 3
Project: hadoop-oss   File: RetryPolicies.java   Source Code and License Vote up 4 votes
@VisibleForTesting
public static String constructReasonString(long maxTime,
    TimeUnit timeUnit) {
  return "retries get failed due to exceeded maximum allowed time (" +
      "in " + timeUnit.toString() + "): " + maxTime;
}