org.ceryle.util
Class ClockImpl

java.lang.Object
  extended by org.ceryle.util.ClockImpl
All Implemented Interfaces:
Clock

public class ClockImpl
extends Object
implements Clock

Implements Clock, providing some general utility methods relating to dates and times, such as a date/time stamp as a String. Since it's rather expensive to generate a timestamp statically, if a lot of timestamps are needed, generating them from a Clock object is recommended.

Since:
JDK1.3
Version:
$Id: ClockImpl.java,v 3.4 2007-06-15 12:09:55 altheim Exp $
Author:
Murray Altheim

Field Summary
static String CDT_FORMAT
          Current date and time format "EEE, MMM d, yyyy 'at' hh:mm:ss a zzz", eg., "Mon, Sep 14 2002 at 14:23:55".
static String DATE_FORMAT
          Simple datestamp format "yyyyMMdd", eg., "20020914".
static String DC_FORMAT
          DC-compatible timestamp format "yyyy-MM-dd", eg., "2002-09-14".
static String FTS_FORMAT
          Friendly timestamp format "yyyy-MM-dd HH:mm:ss", eg., "2002-09-14 14:23:55".
static long[] SEC_PER_FIELD
          Constants for calculating elapsed times.
static String TS_FORMAT
          Timestamp format "yyyy-MM-dd'T'HH:mm:ss", eg., "2002-09-14T14:23:55".
static String TSID_FORMAT
          Timestamp ID format "yyyy-MM-dd'T'HH_mm_ss", eg., "2002-09-14T14_23_55".
 
Fields inherited from interface org.ceryle.util.Clock
FIELD_CENTURY, FIELD_DAY, FIELD_DECADE, FIELD_HOUR, FIELD_MINUTE, FIELD_MONTH, FIELD_SECOND, FIELD_WEEK, FIELD_YEAR
 
Constructor Summary
ClockImpl()
          Default constructor for a ClockImpl object using the UTC timezone.
ClockImpl(int offset)
          Constructor for a ClockImpl object with the provided offset from UTC, where offset is the number of milliseconds to add to UTC to obtain local time.
 
Method Summary
static int convertToGregorianField(int field)
          Convert the provided field index to the GregorianCalendar equivalent.
 String getCurrentDate(int UTCOffset)
          Returns a String representing the current date and time, provided an integer offset from UTC (eg., '-8' for Pacific Standard Time), using a default format.
 String getCurrentDate(int UTCOffset, String dateformat)
          Returns a String representing the current date and time.
 String getCurrentDate(int UTCOffset, String dateformat, Date date)
          Returns a String representing the provided Date.
 String getDateStamp(Date date)
          Returns a String representing either the date parameter or if null, the current date as a timestamp of the form "yyyyMMdd" (for use in filenames).
 String getDCDateStamp()
          Returns a String representing the current date as a timestamp of the Dublin Core-compatible form "yyyy-MM-dd".
 String getDCDateStamp(Date date)
          Returns a String representing the provided date and time as a timestamp of the form "yyyy-MM-dd" in UTC.
static int[] getDurationFromDates(Date startDate, Date endDate, boolean cumulative)
          Return the elapsed time as an int[] array between the start and end extents of the two Dates.
 String getFriendlyTimeStamp(Date date)
          Returns a String representing the provided date and time as a timestamp of the form "yyyy-MM-dd HH:mm:ss" in UTC.
 String getTimeStamp()
          Returns a String representing the current date and time as a timestamp of the form "yyyy-MM-dd'T'HH:mm:ss" in UTC.
 String getTimeStamp(Date date)
          Returns a String representing the provided date and time as a timestamp of the form "yyyy-MM-dd'T'HH:mm:ss" in UTC.
 String getTimeStampId()
          Returns a String representing the current date and time as a timestamp of the form "yyyy-MM-dd'T'HH_mm_ss" in UTC.
 String getTimeStampId(Date date)
          Returns a String representing the provided date and time as a timestamp of the form "yyyy-MM-dd'T'HH_mm_ss" in UTC.
 String getTimeString(long millis)
          Returns a properly-formatted, localized String describing the number of hours, minutes and seconds represented by a long value in milliseconds.
 Date parseTimeStamp(String timeStamp)
          Returns a Date created by parsing the provided String in a timestamp of the form "yyyy-MM-dd'T'HH:mm:ss" in UTC.
 int parseYear(String dateString)
          Returns a year value created by parsing the provided String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DC_FORMAT

public static final String DC_FORMAT
DC-compatible timestamp format "yyyy-MM-dd", eg., "2002-09-14".

See Also:
Constant Field Values

TS_FORMAT

public static final String TS_FORMAT
Timestamp format "yyyy-MM-dd'T'HH:mm:ss", eg., "2002-09-14T14:23:55".

See Also:
Constant Field Values

DATE_FORMAT

public static final String DATE_FORMAT
Simple datestamp format "yyyyMMdd", eg., "20020914".

See Also:
Constant Field Values

FTS_FORMAT

public static final String FTS_FORMAT
Friendly timestamp format "yyyy-MM-dd HH:mm:ss", eg., "2002-09-14 14:23:55".

See Also:
Constant Field Values

TSID_FORMAT

public static final String TSID_FORMAT
Timestamp ID format "yyyy-MM-dd'T'HH_mm_ss", eg., "2002-09-14T14_23_55".

See Also:
Constant Field Values

CDT_FORMAT

public static final String CDT_FORMAT
Current date and time format "EEE, MMM d, yyyy 'at' hh:mm:ss a zzz", eg., "Mon, Sep 14 2002 at 14:23:55".

See Also:
Constant Field Values

SEC_PER_FIELD

public static final long[] SEC_PER_FIELD
Constants for calculating elapsed times.

Constructor Detail

ClockImpl

public ClockImpl()
Default constructor for a ClockImpl object using the UTC timezone.


ClockImpl

public ClockImpl(int offset)
Constructor for a ClockImpl object with the provided offset from UTC, where offset is the number of milliseconds to add to UTC to obtain local time. If offset is zero, the timezone is UTC.

Method Detail

getDCDateStamp

public String getDCDateStamp(Date date)
Returns a String representing the provided date and time as a timestamp of the form "yyyy-MM-dd" in UTC. This can be called with new Date() or null to provide the current date and time.


getTimeStamp

public String getTimeStamp(Date date)
Returns a String representing the provided date and time as a timestamp of the form "yyyy-MM-dd'T'HH:mm:ss" in UTC. This can be called with new Date() or null to provide the current date and time.

Specified by:
getTimeStamp in interface Clock

getFriendlyTimeStamp

public String getFriendlyTimeStamp(Date date)
Returns a String representing the provided date and time as a timestamp of the form "yyyy-MM-dd HH:mm:ss" in UTC. This can be called with new Date() or null to provide the current date and time.

Specified by:
getFriendlyTimeStamp in interface Clock

getDateStamp

public String getDateStamp(Date date)
Returns a String representing either the date parameter or if null, the current date as a timestamp of the form "yyyyMMdd" (for use in filenames).

Specified by:
getDateStamp in interface Clock

parseTimeStamp

public Date parseTimeStamp(String timeStamp)
                    throws ParseException
Returns a Date created by parsing the provided String in a timestamp of the form "yyyy-MM-dd'T'HH:mm:ss" in UTC. new Date() or null to provide the current date and time.

Specified by:
parseTimeStamp in interface Clock
Throws:
ParseException

getTimeStampId

public String getTimeStampId(Date date)
Returns a String representing the provided date and time as a timestamp of the form "yyyy-MM-dd'T'HH_mm_ss" in UTC. This uses characters that are "safe" as XML names, for use in IDs or filenames. This can be called with new Date() or null to provide the current date and time.

Specified by:
getTimeStampId in interface Clock

parseYear

public int parseYear(String dateString)
Returns a year value created by parsing the provided String. This first peeks to see if the first four characters between 1900 and 2100, then tries all of the available DateFormatters, then a pathological search for a four digit number within the String. Returns -1 if unable to parse.

Returns:
a four digit year value or -1 if unable to parse.

getCurrentDate

public String getCurrentDate(int UTCOffset)
Returns a String representing the current date and time, provided an integer offset from UTC (eg., '-8' for Pacific Standard Time), using a default format.

Specified by:
getCurrentDate in interface Clock

getTimeStamp

public String getTimeStamp()
Returns a String representing the current date and time as a timestamp of the form "yyyy-MM-dd'T'HH:mm:ss" in UTC.

Specified by:
getTimeStamp in interface Clock

getTimeStampId

public String getTimeStampId()
Returns a String representing the current date and time as a timestamp of the form "yyyy-MM-dd'T'HH_mm_ss" in UTC. This uses characters that are "safe" as XML names, for use in IDs.

Specified by:
getTimeStampId in interface Clock

getDCDateStamp

public String getDCDateStamp()
Returns a String representing the current date as a timestamp of the Dublin Core-compatible form "yyyy-MM-dd".

Specified by:
getDCDateStamp in interface Clock

getCurrentDate

public String getCurrentDate(int UTCOffset,
                             String dateformat)
Returns a String representing the current date and time. Parameters include an integer offset from UTC (eg., '0' for GMT, '-8' for Pacific Standard Time), and a format string conforming to java.text.SimpleDateFormat

Specified by:
getCurrentDate in interface Clock
See Also:
SimpleDateFormat, GregorianCalendar

getCurrentDate

public String getCurrentDate(int UTCOffset,
                             String dateformat,
                             Date date)
Returns a String representing the provided Date. Parameters include an integer offset from UTC (eg., '0' for GMT, '-8' for Pacific Standard Time), and a format string conforming to java.text.SimpleDateFormat

Specified by:
getCurrentDate in interface Clock
See Also:
SimpleDateFormat, GregorianCalendar

getTimeString

public String getTimeString(long millis)
Returns a properly-formatted, localized String describing the number of hours, minutes and seconds represented by a long value in milliseconds. This relies on the ResourceBundle-based localization in MsgId/Msg, but is not strictly internationalized: i.e., it always returns a String in HH-MM-SS order.

Specified by:
getTimeString in interface Clock

getDurationFromDates

public static int[] getDurationFromDates(Date startDate,
                                         Date endDate,
                                         boolean cumulative)
Return the elapsed time as an int[] array between the start and end extents of the two Dates. The values are as follows: If the value of cumulative is true, the duration will be represented by the entire array of values taken cumulatively, otherwise each field will equal the duration (just expressed in those units). The field constants are the same used in the class TimeLine.

Parameters:
startDate - the start date of the duration.
endDate - the end date of the duration.
cumulative - when true, the duration is expressed by all the returned fields; when false, each field represents the duration.
Returns:
an array of int values representing the duration.
See Also:
TimeLine

convertToGregorianField

public static int convertToGregorianField(int field)
Convert the provided field index to the GregorianCalendar equivalent.



The Ceryle Project. Copyright ©2001-2007 Murray Altheim, All Rights Reserved. See LICENSE included with distribution.