org.osesb.utilities.language
Class DateTimeConversionUtility

java.lang.Object
  extended by org.osesb.utilities.language.DateTimeConversionUtility

public final class DateTimeConversionUtility
extends java.lang.Object

Centralized date-time processing for framework components.

DateTimeConversionUtility provides methods for converting among Strings, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, and java.sql.Timestamp.

DateTimeConversionUtility can be used as an instance object or as a static class:

When DateTimeConversionUtility is constructed, loadDatetimeProperties() will attaempt to load conversion formats according to the following plan:

1. load dateTime formats from a file given by the System Property key SYSTEM_DATETIME_PROPERTIES_FILE

2. load dateTime formats from the default conversion properties file, DEFAULT_DATETIME_PROPERTIES_FILE.

3. use staticly defined default dateTime formats.

However, when DateTimeConversionUtility is used as an instance Object, the configured and default conversion formats can be overridden by using accessors to set conversion formats.

Since:
Version .9

Field Summary
protected  java.text.SimpleDateFormat dateFormatter
          SimpleDateFormat for Dates.
protected  java.util.Properties datetimeProperties
          Date-time Properties that override defaults formats for Date, Time, and Timestamp.
protected  java.lang.String datetimePropertiesFile
          The loaded Properties file, if any.
static java.lang.String DEFAULT_DATE_FORMAT
          Default Date format.
static java.lang.String DEFAULT_DATETIME_PROPERTIES_FILE
          The default name for the dateTime format properties file.
static java.lang.String DEFAULT_TIME_FORMAT
          Default Time format.
static java.lang.String DEFAULT_TIMESTAMP_FORMAT
          Default Timestamp format.
static java.lang.String SYSTEM_DATETIME_PROPERTIES_FILE
          The system property key used to identify the dateTime format properties file.
protected static java.lang.String thisClassName
          The full name of this class.
protected  java.text.SimpleDateFormat timeFormatter
          SimpleDateFormat for Time.
protected  java.text.SimpleDateFormat timestampFormatter
          SimpleDateFormat for Timestamps.
 
Constructor Summary
DateTimeConversionUtility()
          Creates a new instance of DateTimeConversionUtility.
 
Method Summary
static java.util.Date convertCalendarToDate(java.util.Calendar calendar)
          Converts an instance of java.util.Calendar into and instance of java.util.Date.
 java.lang.String convertCalendarToString(java.util.Calendar calendar)
          Converts an instance of java.util.Calendar into a String
static java.util.Calendar convertDateToCalendar(java.util.Date date)
          Converts an instance of java.util.Date into an instance of java.util.Calendar.
 java.lang.String convertDateToString(java.util.Date date)
          Converts an instance of java.util.Date into a String
static java.lang.String convertDateToString(java.util.Date date, java.lang.String dateTimeFormat)
          Converts an instance of java.util.Date into a String
 java.lang.String convertSQLDateToString(java.sql.Date columnValue)
          Converts an java.sql.Date to a String based on the configured format or the default format.
static java.lang.String convertSQLDateToString(java.sql.Date columnValue, java.lang.String dateTimeFormat)
          Converts an java.sql.Date to a String.
 java.lang.String convertSQLTimestampToString(java.sql.Timestamp columnValue)
          Converts an java.sql.Timestamp to a String based on the configured format or the default format.
static java.lang.String convertSQLTimestampToString(java.sql.Timestamp columnValue, java.lang.String dateTimeFormat)
          Converts an java.sql.Timestamp to a String.
 java.lang.String convertSQLTimeToString(java.sql.Time columnValue)
          Converts an java.sql.Time to a String based on the configured format or the default format.
static java.lang.String convertSQLTimeToString(java.sql.Time columnValue, java.lang.String dateTimeFormat)
          Converts an java.sql.Time to a String.
 java.util.Calendar convertStringToCalendar(java.lang.String dateTime)
          Converts a datetime string into and instance of java.util.Calendar
 java.util.Date convertStringToDate(java.lang.String dateTime)
          Converts a datetime string into and instance of java.util.Date
static java.util.Date convertStringToDate(java.lang.String dateTime, java.lang.String dateTimeFormat)
          Converts a datetime string into and instance of java.util.Date using the specified format.
 java.lang.String getDateFormat()
          Return the format for converting between dates and Strings.
 java.lang.String getTimeFormat()
          Return the format for converting between times and Strings.
 java.lang.String getTimestampFormat()
          Return the format for converting between timestamps and Strings.
protected  void loadDatetimeProperties()
          Loads datetimeProperties from a properties file using the following plan: 1.
static void main(java.lang.String[] args)
          main(): For testing and sandboxing.
 void setDateFormat(java.lang.String dateFormat)
          Set the format for converting between dates and Strings.
 void setTimeFormat(java.lang.String timeFormat)
          Set the format for converting between times and Strings.
 void setTimestampFormat(java.lang.String timestampFormat)
          Set the format for converting between timestamps and Strings.
 java.lang.String toString()
          Returns the String representation of the DateTimeConversionUtility.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

thisClassName

protected static java.lang.String thisClassName
The full name of this class.


datetimeProperties

protected java.util.Properties datetimeProperties
Date-time Properties that override defaults formats for Date, Time, and Timestamp.


datetimePropertiesFile

protected java.lang.String datetimePropertiesFile
The loaded Properties file, if any. Used for toString().


DEFAULT_DATETIME_PROPERTIES_FILE

public static final java.lang.String DEFAULT_DATETIME_PROPERTIES_FILE
The default name for the dateTime format properties file.

See Also:
Constant Field Values

SYSTEM_DATETIME_PROPERTIES_FILE

public static final java.lang.String SYSTEM_DATETIME_PROPERTIES_FILE
The system property key used to identify the dateTime format properties file.

See Also:
Constant Field Values

DEFAULT_DATE_FORMAT

public static final java.lang.String DEFAULT_DATE_FORMAT
Default Date format.

See Also:
Constant Field Values

dateFormatter

protected java.text.SimpleDateFormat dateFormatter
SimpleDateFormat for Dates.


DEFAULT_TIME_FORMAT

public static final java.lang.String DEFAULT_TIME_FORMAT
Default Time format.

See Also:
Constant Field Values

timeFormatter

protected java.text.SimpleDateFormat timeFormatter
SimpleDateFormat for Time.


DEFAULT_TIMESTAMP_FORMAT

public static final java.lang.String DEFAULT_TIMESTAMP_FORMAT
Default Timestamp format.

See Also:
Constant Field Values

timestampFormatter

protected java.text.SimpleDateFormat timestampFormatter
SimpleDateFormat for Timestamps.

Constructor Detail

DateTimeConversionUtility

public DateTimeConversionUtility()
Creates a new instance of DateTimeConversionUtility.

Method Detail

loadDatetimeProperties

protected final void loadDatetimeProperties()
Loads datetimeProperties from a properties file using the following plan: 1. Load dateTime format properties from a file given by the System Property key SYSTEM_DATETIME_PROPERTIES_FILE. 2. Load dateTime format properties from the default file, DEFAULT_DATETIME_PROPERTIES_FILE.

Note that this method should not be overridden as it is invoked from this class's constructor.


convertSQLDateToString

public java.lang.String convertSQLDateToString(java.sql.Date columnValue)
                                        throws java.lang.Exception
Converts an java.sql.Date to a String based on the configured format or the default format.

Parameters:
columnValue - The java.sql.Date to convert.
Returns:
the String representation of the Date based on the configured format or the default format.
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertSQLDateToString

public static java.lang.String convertSQLDateToString(java.sql.Date columnValue,
                                                      java.lang.String dateTimeFormat)
                                               throws java.lang.Exception
Converts an java.sql.Date to a String.

Parameters:
columnValue - The java.sql.Date to convert.
dateTimeFormat - The dateTime format to use for the conversion.
Returns:
the String representation of the java.sql.Date based on the given format.
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertSQLTimeToString

public java.lang.String convertSQLTimeToString(java.sql.Time columnValue)
                                        throws java.lang.Exception
Converts an java.sql.Time to a String based on the configured format or the default format.

Parameters:
columnValue - The java.sql.Time to convert.
Returns:
the String representation of the Time based on the configured format or the default format.
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertSQLTimeToString

public static java.lang.String convertSQLTimeToString(java.sql.Time columnValue,
                                                      java.lang.String dateTimeFormat)
                                               throws java.lang.Exception
Converts an java.sql.Time to a String.

Parameters:
columnValue - The java.sql.Timestamp to convert.
dateTimeFormat - The dateTime format to use for the conversion.
Returns:
the String representation of the java.sql.Time based on the given format.
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertSQLTimestampToString

public java.lang.String convertSQLTimestampToString(java.sql.Timestamp columnValue)
                                             throws java.lang.Exception
Converts an java.sql.Timestamp to a String based on the configured format or the default format.

Parameters:
columnValue - The java.sql.Timestamp to convert.
Returns:
the String representation of the Timestamp based on the configured format or the default format.
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertSQLTimestampToString

public static java.lang.String convertSQLTimestampToString(java.sql.Timestamp columnValue,
                                                           java.lang.String dateTimeFormat)
                                                    throws java.lang.Exception
Converts an java.sql.Timestamp to a String.

Parameters:
columnValue - The java.sql.Timestamp to convert.
dateTimeFormat - The dateTime format to use for the conversion.
Returns:
the String representation of the java.sql.Timestamp based on the given format.
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertStringToCalendar

public java.util.Calendar convertStringToCalendar(java.lang.String dateTime)
                                           throws java.lang.Exception
Converts a datetime string into and instance of java.util.Calendar

Parameters:
dateTime - A datetime string.
Returns:
a Calendar object.
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertCalendarToString

public java.lang.String convertCalendarToString(java.util.Calendar calendar)
                                         throws java.lang.Exception
Converts an instance of java.util.Calendar into a String

Parameters:
calendar - An instance of java.util.Calendar.
Returns:
a datetime string
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertStringToDate

public java.util.Date convertStringToDate(java.lang.String dateTime)
                                   throws java.lang.Exception
Converts a datetime string into and instance of java.util.Date

Parameters:
dateTime - A datetime string
Returns:
an instance of java.util.Date
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertStringToDate

public static java.util.Date convertStringToDate(java.lang.String dateTime,
                                                 java.lang.String dateTimeFormat)
                                          throws java.lang.Exception
Converts a datetime string into and instance of java.util.Date using the specified format.

Parameters:
dateTime - A datetime string to convert.
dateTimeFormat - The dateTime format to use for the conversion.
Returns:
an instance of java.util.Date
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertDateToString

public java.lang.String convertDateToString(java.util.Date date)
                                     throws java.lang.Exception
Converts an instance of java.util.Date into a String

Parameters:
date - an Instance of java.util.Date.
Returns:
a datetime string
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertDateToString

public static java.lang.String convertDateToString(java.util.Date date,
                                                   java.lang.String dateTimeFormat)
                                            throws java.lang.Exception
Converts an instance of java.util.Date into a String

Parameters:
date - an Instance of java.util.Date.
dateTimeFormat - The dateTime format to use for the conversion.
Returns:
a datetime string
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertCalendarToDate

public static java.util.Date convertCalendarToDate(java.util.Calendar calendar)
                                            throws java.lang.Exception
Converts an instance of java.util.Calendar into and instance of java.util.Date.

Parameters:
calendar - Instance of java.util.Calendar.
Returns:
Corresponding instance of java.util.Date
Throws:
java.lang.Exception - if an error is encountered during the conversion.

convertDateToCalendar

public static java.util.Calendar convertDateToCalendar(java.util.Date date)
                                                throws java.lang.Exception
Converts an instance of java.util.Date into an instance of java.util.Calendar.

Parameters:
date - Instance of java.util.Date.
Returns:
Corresponding instance of java.util.Calendar
Throws:
java.lang.Exception - if an error is encountered during the conversion.

getDateFormat

public java.lang.String getDateFormat()
Return the format for converting between dates and Strings.

Returns:
The current Date format String

setDateFormat

public void setDateFormat(java.lang.String dateFormat)
                   throws java.lang.Exception
Set the format for converting between dates and Strings.

Parameters:
dateFormat - A valid Date format String.
Throws:
java.lang.Exception - if invoked with an invalid dateFormat.

getTimeFormat

public java.lang.String getTimeFormat()
Return the format for converting between times and Strings.

Returns:
The current Time format String

setTimeFormat

public void setTimeFormat(java.lang.String timeFormat)
                   throws java.lang.Exception
Set the format for converting between times and Strings.

Parameters:
timeFormat - A valid Time format String.
Throws:
java.lang.Exception - if invoked with an invalid timeFormat.

getTimestampFormat

public java.lang.String getTimestampFormat()
Return the format for converting between timestamps and Strings.

Returns:
The current Timestamp format String

setTimestampFormat

public void setTimestampFormat(java.lang.String timestampFormat)
                        throws java.lang.Exception
Set the format for converting between timestamps and Strings.

Parameters:
timestampFormat - A valid Timestamp format String
Throws:
java.lang.Exception - if invoked with an invalid timestampFormat.

toString

public java.lang.String toString()
Returns the String representation of the DateTimeConversionUtility.

Overrides:
toString in class java.lang.Object
Returns:
the String representation of the DateTimeConversionUtility.

main

public static void main(java.lang.String[] args)
main(): For testing and sandboxing.

Parameters:
args - commandline arguments