Class HumanTime

java.lang.Object
com.erudika.para.core.utils.HumanTime
All Implemented Interfaces:
Externalizable, Serializable, Comparable<HumanTime>

public final class HumanTime extends Object implements Externalizable, Comparable<HumanTime>
HumanTime parses and formats time deltas for easier reading by humans. It can format time information without losing information but its main purpose is to generate more easily understood approximations. Using HumanTime

Use HumanTime by creating an instance that contains the time delta (HumanTime(long)), create an empty instance through (HumanTime()) and set the delta using the y(), d(), h(), s() and ms() methods or parse a CharSequence representation (eval(CharSequence)). Parsing ignores whitespace and is case insensitive.

HumanTime format

HumanTime will format time deltas in years ("y"), days ("d"), hours ("h"), minutes ("m"), seconds ("s") and milliseconds ("ms"), separated by a blank character. For approximate representations, the time delta will be round up or down if necessary.

HumanTime examples
  • HumanTime.eval("1 d 1d 2m 3m").getExactly() = "2 d 5 m"
  • HumanTime.eval("2m8d2h4m").getExactly() = "8 d 2 h 6 m"
  • HumanTime.approximately("2 d 8 h 20 m 50 s") = "2 d 8 h"
  • HumanTime.approximately("55m") = "1 h"
Implementation details
  • The time delta can only be increased.
  • Instances of this class are thread safe.
  • Getters using the Java Beans naming conventions are provided for use in environments like JSP or with expression languages like OGNL. See getApproximately() and getExactly().
  • To keep things simple, a year consists of 365 days.
Version:
$Id: HumanTime.java 3906 2011-05-21 13:56:05Z johann $
Author:
Johann Burkard
See Also: