org.xins.common.text
Class DateConverter

java.lang.Object
  extended byorg.xins.common.text.DateConverter

public class DateConverter
extends Object

Utility class for converting a UNIX Epoch date to a human-readable time stamp.

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.

Since:
XINS 1.0.0
Version:
$Revision: 1.44 $ $Date: 2008/07/04 10:22:47 $
Author:
Ernst de Haan, Anthony Goubard

Constructor Summary
DateConverter(boolean withCentury)
          Creates a new DateConverter.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateConverter

public DateConverter(boolean withCentury)
Creates a new DateConverter.

Parameters:
withCentury - true if the century should be in the result, false otherwise.
Since:
XINS 1.3.0
Method Detail

toDateString

public static String toDateString(long time)
                           throws IllegalArgumentException
Convert the specified long to a human-readable time stamp. The current time zone is used.

Parameters:
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.
Returns:
the converted character string, cannot be null.
Throws:
IllegalArgumentException - if n == Long.MIN_VALUE || n == Long.MAX_VALUE.
Since:
XINS 1.5.0

toDateString

public static String toDateString(TimeZone timeZone,
                                  long n)
                           throws IllegalArgumentException
Convert the specified long to a human-readable time stamp.

Parameters:
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.
Returns:
the converted character string, cannot be null.
Throws:
IllegalArgumentException - if n == Long.MIN_VALUE || n == Long.MAX_VALUE || timeZone == null.

format

public String format(long date)
              throws IllegalArgumentException
Formats the specified timestamp as a 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.

Parameters:
date - the timestamp, in milliseconds since the Epoch, must be >= 0L.
Returns:
a character string with the specified date in the correct format.
Throws:
IllegalArgumentException - if date < 0L.
Since:
XINS 1.3.0

format

public void format(long date,
                   char[] buffer,
                   int offset)
            throws IllegalArgumentException,
                   NullPointerException,
                   IndexOutOfBoundsException
Formats the specified timestamp as a string in a character buffer. 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.

Parameters:
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.
Throws:
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).
Since:
XINS 1.3.0


See http://www.xins.org/.