public final class TimeUtils extends Object
This class has been designed with unit tests that need to make
assertions about times relative to "now" in mind. It provides an
interface that defines the notion of "now
". Every instance
of TimeUtils
must provide an implementation of that interface.
The default
instance has an implementation
that always returns System.currentTimeMillis()
.
FakeTimeUtils
Modifier and Type | Class and Description |
---|---|
static interface |
TimeUtils.Now
Defines the notion of "now" as expressed by the implementation
of
TimeUtils.Now.currentTimeMillis() . |
Modifier and Type | Field and Description |
---|---|
static long |
HOURS_PER_DAY
The number of hours in a day.
|
static long |
HUNDREDTHS_PER_MILLIS
The number of milliseconds in a hundredth of a second.
|
static long |
MILLIS_PER_DAY
The number of milliseconds in a day.
|
static long |
MILLIS_PER_HOUR
The number of milliseconds in an hour.
|
static long |
MILLIS_PER_MINUTE
The number of milliseconds in a minute.
|
static long |
MILLIS_PER_SECOND
The number of milliseconds in a second.
|
static long |
MINUTES_PER_HOUR
The number of minutes in an hour.
|
static long |
SECONDS_PER_MINUTE
The number of seconds in a minute.
|
Modifier and Type | Method and Description |
---|---|
long |
currentTimeHundredths()
Returns the current time in hundredths of a second.
|
long |
currentTimeMillis()
Equivalent to invoking
System.currentTimeMillis() . |
long |
currentTimeSecs()
Returns the current time in seconds.
|
Date |
daysAgo(int numDays)
Returns the timestamp of a number of days prior to now.
|
Date |
daysBefore(Date when,
int numDays)
Returns the timestamp of a number of days prior to the given date.
|
String |
formatDateOrTime(Date date)
Returns the given date as a time if it occurred within the
last 24 hours, or as a date if it occurred more
than 24 hours ago.
|
String |
formatInterval(long ms)
Returns a human-friendly string breaking out the specified number of
milliseconds into a representative duration measured in some combination
of days, hours, minutes, and seconds.
|
String |
formatInterval(long ms,
boolean longFormat)
Returns a human-friendly string breaking out the specified number of
milliseconds into a representative duration measured in some combination
of days, hours, minutes, and seconds.
|
static TimeUtils |
getInstance()
Returns the default instance.
|
static TimeUtils |
getInstance(TimeUtils.Now altNow)
Returns a specialized instance that has an alternate implementation
of
TimeUtils.Now . |
String |
hhmmss()
Returns a string representation of the time now, in the form
"hh:mm:ss" . |
String |
hhmmss(long epochMs)
Returns a string representation of the given timestamp, in the form
"hh:mm:ss" . |
String |
hhmmssnnn()
Returns a string representation of the time now, in the form
"hh:mm:ss.nnn" . |
String |
hhmmssnnn(long epochMs)
Returns a string representation of the given timestamp, in the form
"hh:mm:ss.nnn" . |
Date |
hoursAgo(int numHours)
Returns the date timestamp of a number of hours prior to now.
|
Date |
hoursBefore(Date when,
int numHours)
Returns the date timestamp of a number of hours prior to the given date.
|
Date |
makeDate(String s)
Convenience method that will create a
Date instance
corresponding to the date specified in the given string. |
long |
millisSince(Date date)
Returns the number of milliseconds that have elapsed since the
specified date.
|
static String |
rfc822Timestamp(Date date)
Converts a Date into an RFC-822 UTC timestamp String.
|
static String |
rfc822Timestamp(long timeInMillis)
Converts a long representing an offset in milliseconds from the "epoch"
into an RFC-822 UTC timestamp String.
|
static Date |
rfc822Timestamp(String timestamp)
Converts an RFC-822 UTC timestamp String into a Date.
|
long |
upTimeHundredths()
Returns the number of hundredths of seconds since the JVM started up.
|
long |
upTimeMillis()
Returns the number of milliseconds since the JVM started up.
|
long |
upTimeSecs()
Returns the number of seconds since the JVM started up.
|
public static final long HOURS_PER_DAY
public static final long MINUTES_PER_HOUR
public static final long SECONDS_PER_MINUTE
public static final long MILLIS_PER_SECOND
public static final long HUNDREDTHS_PER_MILLIS
public static final long MILLIS_PER_MINUTE
public static final long MILLIS_PER_HOUR
public static final long MILLIS_PER_DAY
public static TimeUtils getInstance()
public static TimeUtils getInstance(TimeUtils.Now altNow)
TimeUtils.Now
. This is designed for use by unit tests that are testing
time-based methods and that want to provide a fully deterministic
notion of "now".altNow
- the alternative implementation of Nowpublic long currentTimeMillis()
System.currentTimeMillis()
.public long currentTimeHundredths()
public long currentTimeSecs()
public long upTimeMillis()
public long upTimeHundredths()
public long upTimeSecs()
public long millisSince(Date date)
date
- the date to examinepublic String formatDateOrTime(Date date)
DateFormat.getTimeInstance()
or
DateFormat.getDateInstance()
as appropriate.date
- the date to formatpublic Date hoursAgo(int numHours)
numHours
- the number of hours before nowpublic Date daysAgo(int numDays)
numDays
- the number of days before nowpublic Date hoursBefore(Date when, int numHours)
when
- the datenumHours
- the number of hours earlierpublic Date daysBefore(Date when, int numDays)
when
- the datenumDays
- the number of days earlierpublic String formatInterval(long ms)
For example if days is nonzero then a day and hour count will be returned, whereas if days is zero but hours isn't then an hour and minute count will be returned; if days and hours are both zero then a minute and second count will be returned.
ms
- time interval in millisecondsIllegalArgumentException
- if ms
is negativepublic String formatInterval(long ms, boolean longFormat)
For example if days is nonzero then a day and hour count will be returned, whereas if days is zero but hours isn't then an hour and minute count will be returned; if days and hours are both zero then a minute and second count will be returned.
If longFormat
is true
, the returned string will spell
out the time element units in full, whereas if false
, the
returned string will use abbreviated units.
ms
- time interval in millisecondslongFormat
- true
spells out time element units;
false
uses abbreviated unitsIllegalArgumentException
- if ms
is negativepublic Date makeDate(String s)
Date
instance
corresponding to the date specified in the given string.
The string must take the form "YYYY-MM-DD"
and must be the
year 2000 or later.s
- the date stringpublic String hhmmss()
"hh:mm:ss"
.public String hhmmss(long epochMs)
"hh:mm:ss"
.epochMs
- epoch time stamppublic String hhmmssnnn()
"hh:mm:ss.nnn"
.public String hhmmssnnn(long epochMs)
"hh:mm:ss.nnn"
.epochMs
- the epoch time stamppublic static String rfc822Timestamp(Date date)
Dates prior to 1 AD (which is the same as 0 BC) are not supported and will result in an IllegalArgumentException.
date
- a Date object to be convertedIllegalArgumentException
- if a date before 0 BC (or 1 AD) is
passedpublic static String rfc822Timestamp(long timeInMillis)
Dates prior to 1 AD (which is the same as 0 BC) are not supported and will result in an IllegalArgumentException.
timeInMillis
- a long representing an offset in milliseconds from
the "epoch"IllegalArgumentException
- if a time before 0 BC (or 1 AD) is
passedpublic static Date rfc822Timestamp(String timestamp) throws ParseException
Given the RFC-822 format, it is not possible to pass a date prior to 1 AD (which is the same as 0 BC).
timestamp
- an RFC-822 UTC timestamp String to be convertedParseException
- if the date
string is not a valid
RFC-822 UTC timestampCopyright © 2015. All Rights Reserved.