- 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 HumanTimeUse HumanTime by creating an instance that contains the time delta (
HumanTime formatHumanTime(long)), create an empty instance through (HumanTime()) and set the delta using they(),d(),h(),s()andms()methods or parse aCharSequencerepresentation (eval(CharSequence)). Parsing ignores whitespace and is case insensitive.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"
- 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()andgetExactly(). - 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:
eval(CharSequence),approximately(CharSequence), Date Formatting and Parsing for Humans in Java with HumanTime, Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Stringapproximately(long l)Formats the given time delta, preserving all data.static Stringapproximately(CharSequence in)Parses and formats the given char sequence, potentially removing some data to make the output easier to understand.intcompareTo(HumanTime t)HumanTimed()Adds one day to the time delta.HumanTimed(int n)Adds n days to the time delta.booleanequals(Object obj)static HumanTimeeval(CharSequence s)Parses aCharSequenceargument and returns aHumanTimeinstance.static Stringexactly(long l)Formats the given time delta, preserving all data.static Stringexactly(CharSequence in)Parses and formats the given char sequence, preserving all data.StringgetApproximately()Returns an approximate, human-formatted representation of the time delta.<T extends Appendable>
TgetApproximately(T a)Appends an approximate, human-formatted representation of the time delta to the givenAppendableobject.longgetDelta()Returns the time delta.StringgetExactly()Returns a human-formatted representation of the time delta.<T extends Appendable>
TgetExactly(T a)Appends a human-formatted representation of the time delta to the givenAppendableobject.HumanTimeh()Adds one hour to the time delta.HumanTimeh(int n)Adds n hours to the time delta.inthashCode()HumanTimem()Adds one month to the time delta.HumanTimem(int n)Adds n months to the time delta.HumanTimems()Adds one millisecond to the time delta.HumanTimems(int n)Adds n milliseconds to the time delta.voidreadExternal(ObjectInput in)HumanTimes()Adds one second to the time delta.HumanTimes(int n)Adds n seconds to the time delta.StringtoString()voidwriteExternal(ObjectOutput out)HumanTimey()Adds one year to the time delta.HumanTimey(int n)Adds n years to the time delta.
-
-
-
Method Detail
-
eval
public static HumanTime eval(CharSequence s)
Parses aCharSequenceargument and returns aHumanTimeinstance.- Parameters:
s- the char sequence, may not benull- Returns:
- an instance, never
null
-
exactly
public static String exactly(CharSequence in)
Parses and formats the given char sequence, preserving all data.Equivalent to
eval(in).getExactly()- Parameters:
in- the char sequence, may not benull- Returns:
- a formatted String, never
null
-
exactly
public static String exactly(long l)
Formats the given time delta, preserving all data.Equivalent to
new HumanTime(in).getExactly()- Parameters:
l- the time delta- Returns:
- a formatted String, never
null
-
approximately
public static String approximately(CharSequence in)
Parses and formats the given char sequence, potentially removing some data to make the output easier to understand.Equivalent to
eval(in).getApproximately()- Parameters:
in- the char sequence, may not benull- Returns:
- a formatted String, never
null
-
approximately
public static String approximately(long l)
Formats the given time delta, preserving all data.Equivalent to
new HumanTime(l).getApproximately()- Parameters:
l- the time delta- Returns:
- a formatted String, never
null
-
y
public HumanTime y()
Adds one year to the time delta.- Returns:
- this HumanTime object
-
y
public HumanTime y(int n)
Adds n years to the time delta.- Parameters:
n- n- Returns:
- this HumanTime object
-
d
public HumanTime d()
Adds one day to the time delta.- Returns:
- this HumanTime object
-
d
public HumanTime d(int n)
Adds n days to the time delta.- Parameters:
n- n- Returns:
- this HumanTime object
-
h
public HumanTime h()
Adds one hour to the time delta.- Returns:
- this HumanTime object
-
h
public HumanTime h(int n)
Adds n hours to the time delta.- Parameters:
n- n- Returns:
- this HumanTime object
-
m
public HumanTime m()
Adds one month to the time delta.- Returns:
- this HumanTime object
-
m
public HumanTime m(int n)
Adds n months to the time delta.- Parameters:
n- n- Returns:
- this HumanTime object
-
s
public HumanTime s()
Adds one second to the time delta.- Returns:
- this HumanTime object
-
s
public HumanTime s(int n)
Adds n seconds to the time delta.- Parameters:
n- seconds- Returns:
- this HumanTime object
-
ms
public HumanTime ms()
Adds one millisecond to the time delta.- Returns:
- this HumanTime object
-
ms
public HumanTime ms(int n)
Adds n milliseconds to the time delta.- Parameters:
n- n- Returns:
- this HumanTime object
-
getExactly
public String getExactly()
Returns a human-formatted representation of the time delta.- Returns:
- a formatted representation of the time delta, never
null
-
getExactly
public <T extends Appendable> T getExactly(T a)
Appends a human-formatted representation of the time delta to the givenAppendableobject.- Type Parameters:
T- the return type- Parameters:
a- the Appendable object, may not benull- Returns:
- the given Appendable object, never
null
-
getApproximately
public String getApproximately()
Returns an approximate, human-formatted representation of the time delta.- Returns:
- a formatted representation of the time delta, never
null
-
getApproximately
public <T extends Appendable> T getApproximately(T a)
Appends an approximate, human-formatted representation of the time delta to the givenAppendableobject.- Type Parameters:
T- the return type- Parameters:
a- the Appendable object, may not benull- Returns:
- the given Appendable object, never
null
-
getDelta
public long getDelta()
Returns the time delta.- Returns:
- the time delta
-
compareTo
public int compareTo(HumanTime t)
- Specified by:
compareToin interfaceComparable<HumanTime>
-
readExternal
public void readExternal(ObjectInput in) throws IOException
- Specified by:
readExternalin interfaceExternalizable- Throws:
IOException
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
- Specified by:
writeExternalin interfaceExternalizable- Throws:
IOException
-
-