org.opensubsystems.core.util
Class DateUtils

java.lang.Object
  extended by org.opensubsystems.core.util.DateUtils

public final class DateUtils
extends java.lang.Object

Collection of useful utilities to work with dates.

Version:
$Id: DateUtils.java,v 1.7 2007/01/07 06:14:00 bastafidli Exp $
Author:
Miro Halas
Code reviewer:
Miro Halas
Code reviewed:
1.5 2005/09/13 13:23:15 bastafidli

Field Summary
static java.lang.String CURRENT_DATE_CODE
          Constant for current date code used in date/time formulas
static java.text.SimpleDateFormat DATE_FORMAT
          static SimpleDateFormat for date format to display on UI and in messages.
static java.text.SimpleDateFormat DATE_SQL_FORMAT
          static SimpleDateFormat for date format for sql date
static java.text.SimpleDateFormat DATE_STORE_FORMAT
          static SimpleDateFormat for date format to store date as string so that it is stored consistently.
static int DATE_TYPE_DATE
          constant for date type DATE
static int DATE_TYPE_DATETIME
          constant for date type DATETIME
static int DATE_TYPE_TIME
          constant for date type TIME
static java.text.SimpleDateFormat DATETIME_FORMAT
          static SimpleDateFormat for datetime format to display on UI and in messages.
static java.text.SimpleDateFormat DATETIME_SQL_FORMAT
          static SimpleDateFormat for datetime format for sql date and time
static java.text.SimpleDateFormat DATETIME_STORE_FORMAT
          static SimpleDateFormat for datetime format to store date and time as string so that it is stored consistently.
static char DAY_CODE
          Constant for dynamic date code used in date/time formulas
static char HOUR_CODE
          Constant for dynamic date code used in date/time formulas
static char MINUTE_CODE
          Constant for dynamic date code used in date/time formulas
static char MONTH_CODE
          Constant for dynamic date code used in date/time formulas
static char NANO_SEPARATOR
          Separator we used to separate time from the nanosecond portion of the timestamp when converted to string.
static long ONE_DAY
          One day in milliseconds.
static long ONE_HOUR
          One hour in milliseconds.
static long ONE_MINUTE
          One minute in milliseconds.
static long ONE_SECOND
          One second in milliseconds.
static char SECOND_CODE
          Constant for dynamic date code used in date/time formulas
static java.text.SimpleDateFormat TIME_FORMAT
          static SimpleDateFormat for time format to display on UI and in messages.
static java.text.SimpleDateFormat TIME_SQL_FORMAT
          static SimpleDateFormat for time format for sql time
static java.text.SimpleDateFormat TIME_STORE_FORMAT
          static SimpleDateFormat for time format to store time as string so that it is stored consistently.
static int TIMING_DAYS
          Constant for timing type
static int TIMING_HOURS
          Constant for timing type
static int TIMING_MINUTES
          Constant for timing type
static int TIMING_MONTHS
          Constant for timing type
static int TIMING_NEVER
          Constant for timing type
static int TIMING_NONE
          Constant for timing type
static int TIMING_WEEKS
          Constant for timing type
static int TIMING_YEARS
          Constant for timing type
static char WEEK_CODE
          Constant for dynamic date code used in date/time formulas
static char YEAR_CODE
          Constant for dynamic date code used in date/time formulas
 
Method Summary
static int comparePeriods(int iPeriodType1, int iPeriodDuration1, int iPeriodType2, int iPeriodDuration2)
          Method to compare time periods
static boolean dateAndTimeEquals(java.util.Date dtFirst, java.util.Date dtSecond)
          Check if two dates and times are equal.
static boolean dateEquals(java.util.Date dtFirst, java.util.Date dtSecond)
          Check if two dates equals regardless of the time.
static java.sql.Timestamp getPeriodExpiration(java.sql.Timestamp tsStartDate, int iPeriodType, int iPeriodDuration)
          Get expiration timestamp from start date, period type and duration.
static java.lang.String getStringTime(long lTimeInMiliseconds)
          Function returns time string in the form MM:SS.MS from the input specified in miliseconds.
static java.lang.String getTimestampAsString(java.sql.Timestamp tsTimestamp)
          Convert timestamp to string including it's nanosecond portion so that it can be safely stored in variable of web page.
static boolean isFunction(java.lang.String strValue)
          Check if String representing date is function or date.
static java.sql.Timestamp parseDateTime(java.lang.String strValue, int iDateType, boolean stored)
          Parse date time value from given string resolving any functions or formulas the string can contain.
static java.lang.String parseDayPeriod(long lPeriod)
          Parse the specified period into string displaying number of days the period represents.
static java.lang.String parseDayTimePeriod(long lPeriod)
          Parse the specified period into string displaying date and time the period represents.
static java.sql.Timestamp parseTimestamp(java.lang.String strTimestamp)
          Convert string to timestamp including if available it's nanosecond portion so that it can be safely restored from variable in web page.
static boolean timeEquals(java.util.Date dtFirst, java.util.Date dtSecond, boolean bIgnoreMilliseconds)
          Check if two times equals regardless of the date.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ONE_SECOND

public static final long ONE_SECOND
One second in milliseconds.

See Also:
Constant Field Values

ONE_MINUTE

public static final long ONE_MINUTE
One minute in milliseconds.

See Also:
Constant Field Values

ONE_HOUR

public static final long ONE_HOUR
One hour in milliseconds.

See Also:
Constant Field Values

ONE_DAY

public static final long ONE_DAY
One day in milliseconds.

See Also:
Constant Field Values

NANO_SEPARATOR

public static final char NANO_SEPARATOR
Separator we used to separate time from the nanosecond portion of the timestamp when converted to string.

See Also:
Constant Field Values

TIMING_NEVER

public static final int TIMING_NEVER
Constant for timing type

See Also:
Constant Field Values

TIMING_MINUTES

public static final int TIMING_MINUTES
Constant for timing type

See Also:
Constant Field Values

TIMING_HOURS

public static final int TIMING_HOURS
Constant for timing type

See Also:
Constant Field Values

TIMING_DAYS

public static final int TIMING_DAYS
Constant for timing type

See Also:
Constant Field Values

TIMING_WEEKS

public static final int TIMING_WEEKS
Constant for timing type

See Also:
Constant Field Values

TIMING_MONTHS

public static final int TIMING_MONTHS
Constant for timing type

See Also:
Constant Field Values

TIMING_YEARS

public static final int TIMING_YEARS
Constant for timing type

See Also:
Constant Field Values

TIMING_NONE

public static final int TIMING_NONE
Constant for timing type

See Also:
Constant Field Values

CURRENT_DATE_CODE

public static final java.lang.String CURRENT_DATE_CODE
Constant for current date code used in date/time formulas

See Also:
Constant Field Values

YEAR_CODE

public static final char YEAR_CODE
Constant for dynamic date code used in date/time formulas

See Also:
Constant Field Values

MONTH_CODE

public static final char MONTH_CODE
Constant for dynamic date code used in date/time formulas

See Also:
Constant Field Values

WEEK_CODE

public static final char WEEK_CODE
Constant for dynamic date code used in date/time formulas

See Also:
Constant Field Values

DAY_CODE

public static final char DAY_CODE
Constant for dynamic date code used in date/time formulas

See Also:
Constant Field Values

HOUR_CODE

public static final char HOUR_CODE
Constant for dynamic date code used in date/time formulas

See Also:
Constant Field Values

MINUTE_CODE

public static final char MINUTE_CODE
Constant for dynamic date code used in date/time formulas

See Also:
Constant Field Values

SECOND_CODE

public static final char SECOND_CODE
Constant for dynamic date code used in date/time formulas

See Also:
Constant Field Values

DATE_TYPE_DATE

public static final int DATE_TYPE_DATE
constant for date type DATE

See Also:
Constant Field Values

DATE_TYPE_TIME

public static final int DATE_TYPE_TIME
constant for date type TIME

See Also:
Constant Field Values

DATE_TYPE_DATETIME

public static final int DATE_TYPE_DATETIME
constant for date type DATETIME

See Also:
Constant Field Values

DATE_FORMAT

public static final java.text.SimpleDateFormat DATE_FORMAT
static SimpleDateFormat for date format to display on UI and in messages.


TIME_FORMAT

public static final java.text.SimpleDateFormat TIME_FORMAT
static SimpleDateFormat for time format to display on UI and in messages.


DATETIME_FORMAT

public static final java.text.SimpleDateFormat DATETIME_FORMAT
static SimpleDateFormat for datetime format to display on UI and in messages.


DATE_STORE_FORMAT

public static final java.text.SimpleDateFormat DATE_STORE_FORMAT
static SimpleDateFormat for date format to store date as string so that it is stored consistently.


TIME_STORE_FORMAT

public static final java.text.SimpleDateFormat TIME_STORE_FORMAT
static SimpleDateFormat for time format to store time as string so that it is stored consistently.


DATETIME_STORE_FORMAT

public static final java.text.SimpleDateFormat DATETIME_STORE_FORMAT
static SimpleDateFormat for datetime format to store date and time as string so that it is stored consistently.


DATE_SQL_FORMAT

public static final java.text.SimpleDateFormat DATE_SQL_FORMAT
static SimpleDateFormat for date format for sql date


TIME_SQL_FORMAT

public static final java.text.SimpleDateFormat TIME_SQL_FORMAT
static SimpleDateFormat for time format for sql time


DATETIME_SQL_FORMAT

public static final java.text.SimpleDateFormat DATETIME_SQL_FORMAT
static SimpleDateFormat for datetime format for sql date and time

Method Detail

dateEquals

public static boolean dateEquals(java.util.Date dtFirst,
                                 java.util.Date dtSecond)
Check if two dates equals regardless of the time. Two null dates are equal. Null and not null dates are not equal.

Parameters:
dtFirst - - first date to compare, can be null
dtSecond - - second date to compare, can be null
Returns:
boolean - true if two dates equals regardless of what the time is

timeEquals

public static boolean timeEquals(java.util.Date dtFirst,
                                 java.util.Date dtSecond,
                                 boolean bIgnoreMilliseconds)
Check if two times equals regardless of the date. Two null times are equal. Null and not null times are not equal.

Parameters:
dtFirst - - first time to compare, can be null
dtSecond - - second time to compare, can be null
bIgnoreMilliseconds - - if true milliseconds will be ignored in comparison
Returns:
boolean - true if two time equals regardless of what the date is

dateAndTimeEquals

public static boolean dateAndTimeEquals(java.util.Date dtFirst,
                                        java.util.Date dtSecond)
Check if two dates and times are equal. Two null dates are equal. Null and not null dates are not equal.

Parameters:
dtFirst - - first date time to compare, can be null
dtSecond - - second date time to compare, can be null
Returns:
boolean - true if two date and times are equal

isFunction

public static boolean isFunction(java.lang.String strValue)
Check if String representing date is function or date. Date is a function (formula) if it starts with the current date/time variable which can be followed by expression describing period from current date.

Parameters:
strValue - - string representation of date or date function
Returns:
boolean - date function flag

parseDateTime

public static java.sql.Timestamp parseDateTime(java.lang.String strValue,
                                               int iDateType,
                                               boolean stored)
                                        throws OSSInvalidDataException
Parse date time value from given string resolving any functions or formulas the string can contain. This method can be therefore used if the passed string contains string representation date, time or timestamp or a formula such as now + 3h - 1m + 4d.

Parameters:
strValue - - string representation of date or date function
iDateType - - date type code, one of the DATE_TYPE_XXX constants
stored - - flag if Date should be parsed using format used for storage or for display
Returns:
Timestamp - parsed date or null if date was null
Throws:
OSSInvalidDataException - - error during parsing

parseDayPeriod

public static java.lang.String parseDayPeriod(long lPeriod)
Parse the specified period into string displaying number of days the period represents.

Parameters:
lPeriod - - period in miliseconds
Returns:
String - period in format 'x day(s)' or '' if not valid period

parseDayTimePeriod

public static java.lang.String parseDayTimePeriod(long lPeriod)
Parse the specified period into string displaying date and time the period represents.

Parameters:
lPeriod - - preiod in miliseconds
Returns:
String - period in format 'x day(s) y hour(s) z minute(s)' or '' if not valid period

getPeriodExpiration

public static java.sql.Timestamp getPeriodExpiration(java.sql.Timestamp tsStartDate,
                                                     int iPeriodType,
                                                     int iPeriodDuration)
Get expiration timestamp from start date, period type and duration. For example if the start date is now, period type is hour and duration is 2 then the result will be timestamp representing now + 2 hours.

Parameters:
tsStartDate - - start date of period counting
iPeriodType - - one of the period type constant TIMING_XXX
iPeriodDuration - - period duration, number of time units specified by period type
Returns:
Timestamp - date of period expiration or null if any problem

comparePeriods

public static int comparePeriods(int iPeriodType1,
                                 int iPeriodDuration1,
                                 int iPeriodType2,
                                 int iPeriodDuration2)
Method to compare time periods

Parameters:
iPeriodType1 - - first period type, one of the period type constant TIMING_XXX
iPeriodDuration1 - - first period duration
iPeriodType2 - - second period type, one of the period type constant TIMING_XXX
iPeriodDuration2 - - second period duration
Returns:
int - 1 - first period is longer 0 - periods are same -1 - first period is shorter

getTimestampAsString

public static java.lang.String getTimestampAsString(java.sql.Timestamp tsTimestamp)
Convert timestamp to string including it's nanosecond portion so that it can be safely stored in variable of web page.

Parameters:
tsTimestamp - - timestamp to convert
Returns:
String - text containing time and nanosecond portion of timestamp

parseTimestamp

public static java.sql.Timestamp parseTimestamp(java.lang.String strTimestamp)
                                         throws java.lang.NumberFormatException
Convert string to timestamp including if available it's nanosecond portion so that it can be safely restored from variable in web page.

Parameters:
strTimestamp - - timestamp to convert
Returns:
Timestamp - restored timestamp
Throws:
java.lang.NumberFormatException - - problem parsing the string

getStringTime

public static java.lang.String getStringTime(long lTimeInMiliseconds)
Function returns time string in the form MM:SS.MS from the input specified in miliseconds.

Parameters:
lTimeInMiliseconds - - time in miliseconds
Returns:
String - string representation of miliseconds in the form MM:SS.MS


Copyright © 2003 - 2006 OpenSubsystems s.r.o.