- All Implemented Interfaces:
Externalizable
,Serializable
,Comparable<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 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:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
approximately
(long l) Formats the given time delta, preserving all data.static String
Parses and formats the given char sequence, potentially removing some data to make the output easier to understand.int
d()
Adds one day to the time delta.d
(int n) Adds n days to the time delta.boolean
static HumanTime
eval
(CharSequence s) Parses aCharSequence
argument and returns aHumanTime
instance.static String
exactly
(long l) Formats the given time delta, preserving all data.static String
exactly
(CharSequence in) Parses and formats the given char sequence, preserving all data.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 givenAppendable
object.long
getDelta()
Returns the time delta.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 givenAppendable
object.h()
Adds one hour to the time delta.h
(int n) Adds n hours to the time delta.int
hashCode()
m()
Adds one month to the time delta.m
(int n) Adds n months to the time delta.ms()
Adds one millisecond to the time delta.ms
(int n) Adds n milliseconds to the time delta.void
s()
Adds one second to the time delta.s
(int n) Adds n seconds to the time delta.toString()
void
y()
Adds one year to the time delta.y
(int n) Adds n years to the time delta.
-
Constructor Details
-
HumanTime
public HumanTime()No-argument Constructor for HumanTime.Equivalent to calling
new HumanTime(0L)
. -
HumanTime
public HumanTime(long delta) Constructor for HumanTime.- Parameters:
delta
- the initial time delta, interpreted as a positive number
-
-
Method Details
-
eval
Parses aCharSequence
argument and returns aHumanTime
instance.- Parameters:
s
- the char sequence, may not benull
- Returns:
- an instance, never
null
-
exactly
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
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
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
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
-
y
Adds n years to the time delta.- Parameters:
n
- n- Returns:
- this HumanTime object
-
d
-
d
Adds n days to the time delta.- Parameters:
n
- n- Returns:
- this HumanTime object
-
h
-
h
Adds n hours to the time delta.- Parameters:
n
- n- Returns:
- this HumanTime object
-
m
-
m
Adds n months to the time delta.- Parameters:
n
- n- Returns:
- this HumanTime object
-
s
-
s
Adds n seconds to the time delta.- Parameters:
n
- seconds- Returns:
- this HumanTime object
-
ms
-
ms
Adds n milliseconds to the time delta.- Parameters:
n
- n- Returns:
- this HumanTime object
-
getExactly
Returns a human-formatted representation of the time delta.- Returns:
- a formatted representation of the time delta, never
null
-
getExactly
Appends a human-formatted representation of the time delta to the givenAppendable
object.- Type Parameters:
T
- the return type- Parameters:
a
- the Appendable object, may not benull
- Returns:
- the given Appendable object, never
null
-
getApproximately
Returns an approximate, human-formatted representation of the time delta.- Returns:
- a formatted representation of the time delta, never
null
-
getApproximately
Appends an approximate, human-formatted representation of the time delta to the givenAppendable
object.- 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
-
equals
-
hashCode
-
toString
-
compareTo
- Specified by:
compareTo
in interfaceComparable<HumanTime>
-
readExternal
- Specified by:
readExternal
in interfaceExternalizable
- Throws:
IOException
-
writeExternal
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-