public class DateConverter extends Object
For example, the date 26 July 2003, time 17:03, 59 seconds and 653
milliseconds will convert to the string
"2003.07.26 17:03:59.653"
.
See the Wikipedia article about the UNIX Epoch for more information.
Constructor and Description |
---|
DateConverter(boolean withCentury)
Creates a new
DateConverter . |
Modifier and Type | Method and Description |
---|---|
String |
format(long date)
Formats the specified timestamp as a
String . |
void |
format(long date,
char[] buffer,
int offset)
Formats the specified timestamp as a string in a character buffer.
|
static String |
toDateString(long time)
Convert the specified
long to a human-readable time stamp. |
static String |
toDateString(TimeZone timeZone,
long n)
Convert the specified
long to a human-readable time stamp. |
public DateConverter(boolean withCentury)
DateConverter
.withCentury
- true
if the century should be in the result,
false
otherwise.public static String toDateString(long time) throws IllegalArgumentException
long
to a human-readable time stamp.
The current time zone is used.time
- the time stamp to be converted to a human-readable character string,
as a number of milliseconds since the
UNIX Epoch.
must be greater than Long.MIN_VALUE
and smaller than
Long.MAX_VALUE
.null
.IllegalArgumentException
- if n == Long.MIN_VALUE
|| n == Long.MAX_VALUE
.public static String toDateString(TimeZone timeZone, long n) throws IllegalArgumentException
long
to a human-readable time stamp.timeZone
- the time zone to use, cannot be null
.n
- the time stamp to be converted to a human-readable character string,
as a number of milliseconds since the Epoch (midnight January 1,
1970), must be greater than Long.MIN_VALUE
and smaller than
Long.MAX_VALUE
.null
.IllegalArgumentException
- if n == Long.MIN_VALUE
|| n == Long.MAX_VALUE
|| timeZone == null
.public String format(long date) throws IllegalArgumentException
String
. Depending on
the setting of the withCentury property (passed to the
constructor), the format is as follows. If withCentury is set,
then the format is:
CCYYMMDD-hhmmssSSS (length is 18)Otherwise, if withCentury is not set, then the format is without the century:
YYMMDD-hhmmssSSS (length is 16)
The timestamp is a number of milliseconds since the Epoch (midnight
at the start of January 1, 1970, GMT). See
System.currentTimeMillis()
.
Note: This method is not thread-safe.
date
- the timestamp, in milliseconds since the Epoch, must be >=
0L
.IllegalArgumentException
- if date < 0L
.public void format(long date, char[] buffer, int offset) throws IllegalArgumentException, NullPointerException, IndexOutOfBoundsException
CCYYMMDD-hhmmssSSS (length is 18)Otherwise, if withCentury is not set, then the format is without the century:
YYMMDD-hhmmssSSS (length is 16)
The timestamp is a number of milliseconds since the Epoch (midnight
at the start of January 1, 1970, GMT). See
System.currentTimeMillis()
.
Note: This method is not thread-safe.
date
- the timestamp, in milliseconds since the Epoch, must be >=
0L
.buffer
- the character buffer to put the formatted timestamp in, cannot be
null
.offset
- the offset into the character buffer.IllegalArgumentException
- if date < 0L
.NullPointerException
- if buffer == null
.IndexOutOfBoundsException
- if offset
is invalid (less than 0
) or
incorrect (not leaving enough room for the formatter date).See http://www.xins.org/.