Package adams.core
Class DateUtils
- java.lang.Object
-
- adams.core.DateUtils
-
public class DateUtils extends Object
A helper class for common Date-related operations.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
-
-
Field Summary
Fields Modifier and Type Field Description static int
BAD_DATE
static long
DAY_MILLISECONDS
static int
HOURS_PER_DAY
protected static Locale
m_Locale
the locale to use.protected static TimeZone
m_TimeZone
the timezone to use.static int
MINUTES_PER_HOUR
static int
SECONDS_PER_DAY
static int
SECONDS_PER_MINUTE
-
Constructor Summary
Constructors Constructor Description DateUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static int
absoluteDay(Calendar cal, boolean use1904windowing)
Given a Calendar, return the number of days since 1900/12/31.static boolean
checkDate(String s)
Checks whether the string represents a date.static boolean
checkDate(String s, DateFormat df)
Checks whether the string represents a date.static boolean
checkDateTime(String s)
Checks whether the string represents a date/time.static boolean
checkDateTime(String s, DateFormat df)
Checks whether the string represents a date/time.static boolean
checkDateTimeMsec(String s)
Checks whether the string represents a date/time msec.static boolean
checkDateTimeMsec(String s, DateFormat df)
Checks whether the string represents a date/time msec.static boolean
checkTime(String s)
Checks whether the string represents a time.static boolean
checkTime(String s, DateFormat df)
Checks whether the string represents a time.static boolean
checkTimeMsec(String s)
Checks whether the string represents a time with msecs.static boolean
checkTimeMsec(String s, DateFormat df)
Checks whether the string represents a time with msecs.protected static int
daysInPriorYears(int yr, boolean use1904windowing)
Return the number of days in prior years since 1900protected static Calendar
dayStart(Calendar cal)
static Long
difference(Date older, Date newer)
Returns the difference in time.static Double
differenceDays(Date older, Date newer)
Returns the difference in time in days.static Double
differenceHours(Date older, Date newer)
Returns the difference in time in hours.static Double
differenceMinutes(Date older, Date newer)
Returns the difference in time in minutes.static Double
differenceSeconds(Date older, Date newer)
Returns the difference in time in seconds.static Date
earlier(Date date, int type, int decrement)
Returns a date earlier relative to the provided one, e.g., earlier by 2 hours: earlier(date, Calendar.HOUR, 2).static Calendar
getCalendar()
Returns a newGregorianCalendar
object with the system-wide defined locale and time zone.static DateFormat
getDateFormatter()
Returns a date formatting object, initialized with date format.protected static Calendar
getJavaCalendar(double date, boolean use1904windowing, TimeZone timeZone, boolean roundSeconds)
Get EXCEL date as Java Calendar with given time zone.static Locale
getLocale()
Returns the locale in use.static DateFormat
getTimeFormatter()
Returns a time formatting object, initialized with time format.static DateFormat
getTimeFormatterMsecs()
Returns a time formatting object, initialized with time format (incl msecs).static DateFormat
getTimestampFormatter()
Returns a date formatting object, initialized with timestamp format.static DateFormat
getTimestampFormatterMsecs()
Returns a date formatting object, initialized with timestamp format that sports milliseconds.static TimeZone
getTimeZone()
Returns the time zone in use.protected static double
internalGetExcelDate(Calendar date, boolean use1904windowing)
static boolean
isAfter(Date base, Date check)
Returns whether the "check" date is after the "base" date.static boolean
isBefore(Date base, Date check)
Returns whether the "check" date is before the "base" date.static boolean
isValid(String pattern)
Checks whether the supplied parse pattern is valid.protected static boolean
isValidExcelDate(double value)
Given a double, checks if it is a valid Excel date.static Date
later(Date date, int type, int increment)
Returns a date later relative to the provided one, e.g., later by 2 hours: later(date, Calendar.HOUR, 2).static double
msecToSerialDate(long msec)
Converts the milli-seconds (Java Epoch) to a Excel serial date (days since 0-jan-1900).static double
msecToSerialDate(Date date)
Converts the date to a Excel serial date (days since 0-jan-1900).static String
msecToString(Long msec)
Turns the milli-seconds into a string: hh:mm:ss.sssstatic DateTime
now()
Returns a new DateTime object with the current date/time.static Time
nowTime()
Returns a new Time object with the current time.static Date
parseString(String s, DateFormatString format)
Parses the given date/time string according to the format.static Date
parseString(String s, String format)
Parses the given date/time string according to the format.static Date
serialDateToDate(double serial)
Converts the Excel serial date (days since 0-jan-1900) to a Java date.static long
serialDateToMsec(double serial)
Converts the Excel serial date (days since 0-jan-1900) to milli-seconds (Java Epoch).static Date
set(Date date, int type, int value)
Returns a date with a value changed, e.g., the hours set to a specific value.protected static void
setCalendar(Calendar calendar, int wholeDays, int millisecondsInDay, boolean use1904windowing, boolean roundSeconds)
static void
setLocale(Locale value)
Sets the locale to use globally.static void
setTimeZone(TimeZone value)
Sets the time zone to use globally.static Date
today()
Returns a Date object of today, at the start of the day (00:00:00.000).static Date
tomorrow()
Returns a Date object of tomorrow, at the start of the day (00:00:00.000).static Date
yesterday()
Returns a Date object of yesterday, at the start of the day (00:00:00.000).
-
-
-
Field Detail
-
m_TimeZone
protected static TimeZone m_TimeZone
the timezone to use.
-
m_Locale
protected static Locale m_Locale
the locale to use.
-
BAD_DATE
public static final int BAD_DATE
- See Also:
- Constant Field Values
-
SECONDS_PER_MINUTE
public static final int SECONDS_PER_MINUTE
- See Also:
- Constant Field Values
-
MINUTES_PER_HOUR
public static final int MINUTES_PER_HOUR
- See Also:
- Constant Field Values
-
HOURS_PER_DAY
public static final int HOURS_PER_DAY
- See Also:
- Constant Field Values
-
SECONDS_PER_DAY
public static final int SECONDS_PER_DAY
- See Also:
- Constant Field Values
-
DAY_MILLISECONDS
public static final long DAY_MILLISECONDS
- See Also:
- Constant Field Values
-
-
Method Detail
-
setTimeZone
public static void setTimeZone(TimeZone value)
Sets the time zone to use globally.- Parameters:
value
- the time zone
-
getTimeZone
public static TimeZone getTimeZone()
Returns the time zone in use.- Returns:
- the time zone
-
setLocale
public static void setLocale(Locale value)
Sets the locale to use globally.- Parameters:
value
- the locale
-
getLocale
public static Locale getLocale()
Returns the locale in use.- Returns:
- the locale
-
getCalendar
public static Calendar getCalendar()
Returns a newGregorianCalendar
object with the system-wide defined locale and time zone.- Returns:
- the calendar
-
getTimestampFormatter
public static DateFormat getTimestampFormatter()
Returns a date formatting object, initialized with timestamp format.- Returns:
- the formatter
- See Also:
Constants.TIMESTAMP_FORMAT
-
getTimestampFormatterMsecs
public static DateFormat getTimestampFormatterMsecs()
Returns a date formatting object, initialized with timestamp format that sports milliseconds.- Returns:
- the formatter
- See Also:
Constants.TIMESTAMP_FORMAT_MSECS
-
getTimeFormatter
public static DateFormat getTimeFormatter()
Returns a time formatting object, initialized with time format.- Returns:
- the formatter
- See Also:
Constants.TIME_FORMAT
-
getTimeFormatterMsecs
public static DateFormat getTimeFormatterMsecs()
Returns a time formatting object, initialized with time format (incl msecs).- Returns:
- the formatter
- See Also:
Constants.TIME_FORMAT_MSECS
-
getDateFormatter
public static DateFormat getDateFormatter()
Returns a date formatting object, initialized with date format.- Returns:
- the formatter
- See Also:
Constants.DATE_FORMAT
-
isValid
public static boolean isValid(String pattern)
Checks whether the supplied parse pattern is valid.- Parameters:
pattern
- the pattern to check- Returns:
- true if valid
-
earlier
public static Date earlier(Date date, int type, int decrement)
Returns a date earlier relative to the provided one, e.g., earlier by 2 hours: earlier(date, Calendar.HOUR, 2).- Parameters:
date
- the date to use as basistype
- the type of decrement, e.g., Calendar.HOURdecrement
- the amount to decrement the date with- Returns:
- the new date
- See Also:
Calendar
-
later
public static Date later(Date date, int type, int increment)
Returns a date later relative to the provided one, e.g., later by 2 hours: later(date, Calendar.HOUR, 2).- Parameters:
date
- the date to use as basistype
- the type of increment, e.g., Calendar.HOURincrement
- the amount to increment the date with- Returns:
- the new date
- See Also:
Calendar
-
set
public static Date set(Date date, int type, int value)
Returns a date with a value changed, e.g., the hours set to a specific value.- Parameters:
date
- the date to use as basistype
- the type of value to change, e.g., Calendar.HOURvalue
- the value to set- Returns:
- the new date, null in case of an error
- See Also:
Calendar
-
isBefore
public static boolean isBefore(Date base, Date check)
Returns whether the "check" date is before the "base" date.- Parameters:
base
- the basis of the comparisoncheck
- the date to compare with- Returns:
- true if "check" is before "base"
-
isAfter
public static boolean isAfter(Date base, Date check)
Returns whether the "check" date is after the "base" date.- Parameters:
base
- the basis of the comparisoncheck
- the date to compare with- Returns:
- true if "check" is after "base"
-
difference
public static Long difference(Date older, Date newer)
Returns the difference in time.- Parameters:
older
- the older timestampnewer
- the new timestamp- Returns:
- the time difference in msec, null if "older < newer" does not hold
-
differenceDays
public static Double differenceDays(Date older, Date newer)
Returns the difference in time in days.- Parameters:
older
- the older timestampnewer
- the new timestamp- Returns:
- the time difference in days, null if "older < newer" does not hold
-
differenceHours
public static Double differenceHours(Date older, Date newer)
Returns the difference in time in hours.- Parameters:
older
- the older timestampnewer
- the new timestamp- Returns:
- the time difference in hours, null if "older < newer" does not hold
-
differenceMinutes
public static Double differenceMinutes(Date older, Date newer)
Returns the difference in time in minutes.- Parameters:
older
- the older timestampnewer
- the new timestamp- Returns:
- the time difference in minutes, null if "older < newer" does not hold
-
differenceSeconds
public static Double differenceSeconds(Date older, Date newer)
Returns the difference in time in seconds.- Parameters:
older
- the older timestampnewer
- the new timestamp- Returns:
- the time difference in seconds, null if "older < newer" does not hold
-
msecToString
public static String msecToString(Long msec)
Turns the milli-seconds into a string: hh:mm:ss.sss- Parameters:
msec
- the milli-seconds to convert- Returns:
- the string representation
-
parseString
public static Date parseString(String s, String format)
Parses the given date/time string according to the format.- Parameters:
s
- the date/time string to parseformat
- the format to use for parsing- Returns:
- the parsed date or null in case of an error
-
parseString
public static Date parseString(String s, DateFormatString format)
Parses the given date/time string according to the format.- Parameters:
s
- the date/time string to parseformat
- the format to use for parsing- Returns:
- the parsed date or null in case of an error
-
now
public static DateTime now()
Returns a new DateTime object with the current date/time.- Returns:
- the current date/time
-
nowTime
public static Time nowTime()
Returns a new Time object with the current time.- Returns:
- the current time
-
today
public static Date today()
Returns a Date object of today, at the start of the day (00:00:00.000).- Returns:
- the current date
-
tomorrow
public static Date tomorrow()
Returns a Date object of tomorrow, at the start of the day (00:00:00.000).- Returns:
- the date
-
yesterday
public static Date yesterday()
Returns a Date object of yesterday, at the start of the day (00:00:00.000).- Returns:
- the date
-
absoluteDay
protected static int absoluteDay(Calendar cal, boolean use1904windowing)
Given a Calendar, return the number of days since 1900/12/31.- Parameters:
cal
- the Calendar- Returns:
- days number of days since 1900/12/31
- Throws:
IllegalArgumentException
- if date is invalid
-
daysInPriorYears
protected static int daysInPriorYears(int yr, boolean use1904windowing)
Return the number of days in prior years since 1900- Parameters:
yr
- a year (1900 < yr < 4000)use1904windowing
-- Returns:
- days number of days in years prior to yr.
- Throws:
IllegalArgumentException
- if year is outside of range.
-
internalGetExcelDate
protected static double internalGetExcelDate(Calendar date, boolean use1904windowing)
-
msecToSerialDate
public static double msecToSerialDate(long msec)
Converts the milli-seconds (Java Epoch) to a Excel serial date (days since 0-jan-1900).- Parameters:
msec
- the java epoch in msec- Returns:
- the serial date
-
msecToSerialDate
public static double msecToSerialDate(Date date)
Converts the date to a Excel serial date (days since 0-jan-1900).- Parameters:
date
- the date- Returns:
- the serial date
-
isValidExcelDate
protected static boolean isValidExcelDate(double value)
Given a double, checks if it is a valid Excel date.- Parameters:
value
- the double value- Returns:
- true if valid
-
setCalendar
protected static void setCalendar(Calendar calendar, int wholeDays, int millisecondsInDay, boolean use1904windowing, boolean roundSeconds)
-
getJavaCalendar
protected static Calendar getJavaCalendar(double date, boolean use1904windowing, TimeZone timeZone, boolean roundSeconds)
Get EXCEL date as Java Calendar with given time zone.- Parameters:
date
- The Excel date.use1904windowing
- true if date uses 1904 windowing, or false if using 1900 date windowing.timeZone
- The TimeZone to evaluate the date inroundSeconds
- round to closest second- Returns:
- Java representation of the date, or null if date is not a valid Excel date
-
serialDateToDate
public static Date serialDateToDate(double serial)
Converts the Excel serial date (days since 0-jan-1900) to a Java date.- Parameters:
serial
- the serial date- Returns:
- the date
-
serialDateToMsec
public static long serialDateToMsec(double serial)
Converts the Excel serial date (days since 0-jan-1900) to milli-seconds (Java Epoch).- Parameters:
serial
- the serial date- Returns:
- the java epoch in msec
-
checkTime
public static boolean checkTime(String s)
Checks whether the string represents a time.- Parameters:
s
- the string to check- Returns:
- true if time
-
checkTime
public static boolean checkTime(String s, DateFormat df)
Checks whether the string represents a time.- Parameters:
s
- the string to checkdf
- the format, can be null- Returns:
- true if time
-
checkTimeMsec
public static boolean checkTimeMsec(String s)
Checks whether the string represents a time with msecs.- Parameters:
s
- the string to check- Returns:
- true if time with msecs
-
checkTimeMsec
public static boolean checkTimeMsec(String s, DateFormat df)
Checks whether the string represents a time with msecs.- Parameters:
s
- the string to checkdf
- the format, can be null- Returns:
- true if time with msecs
-
checkDate
public static boolean checkDate(String s)
Checks whether the string represents a date.- Parameters:
s
- the string to check- Returns:
- true if date
-
checkDate
public static boolean checkDate(String s, DateFormat df)
Checks whether the string represents a date.- Parameters:
s
- the string to checkdf
- the format, can be null- Returns:
- true if date
-
checkDateTime
public static boolean checkDateTime(String s)
Checks whether the string represents a date/time.- Parameters:
s
- the string to check- Returns:
- true if date/time
-
checkDateTime
public static boolean checkDateTime(String s, DateFormat df)
Checks whether the string represents a date/time.- Parameters:
s
- the string to checkdf
- the format, can be null- Returns:
- true if date/time
-
checkDateTimeMsec
public static boolean checkDateTimeMsec(String s)
Checks whether the string represents a date/time msec.- Parameters:
s
- the string to check- Returns:
- true if date/time msec
-
checkDateTimeMsec
public static boolean checkDateTimeMsec(String s, DateFormat df)
Checks whether the string represents a date/time msec.- Parameters:
s
- the string to checkdf
- the format, can be null- Returns:
- true if date/time msec
-
-