org.h2.util
Class DateTimeUtils

java.lang.Object
  extended by org.h2.util.DateTimeUtils

public class DateTimeUtils
extends java.lang.Object

This utility class contains time conversion functions.

Date value: a bit field with bits for the year, month, and day. Absolute day: the day number (0 means 1970-01-01).


Field Summary
static long MILLIS_PER_DAY
          The number of milliseconds per day.
 
Method Summary
static long absoluteDayFromDateValue(long dateValue)
          Calculate the absolute day from a date value.
static ValueDate convertDate(java.sql.Date x, java.util.Calendar calendar)
          Convert the date using the specified calendar.
static java.sql.Date convertDate(Value value, java.util.Calendar calendar)
          Convert the date to the specified time zone.
static java.sql.Date convertDateValueToDate(long dateValue)
          Convert a date value to a date, using the default timezone.
static java.sql.Timestamp convertDateValueToTimestamp(long dateValue, long nanos)
          Convert a date value / time value to a timestamp, using the default timezone.
static java.sql.Time convertNanoToTime(long nanos)
          Convert a time value to a time, using the default timezone.
static ValueTime convertTime(java.sql.Time x, java.util.Calendar calendar)
          Convert the time using the specified calendar.
static java.sql.Time convertTime(Value value, java.util.Calendar calendar)
          Convert the time to the specified time zone.
static ValueTimestamp convertTimestamp(java.sql.Timestamp x, java.util.Calendar calendar)
          Convert the timestamp using the specified calendar.
static java.sql.Timestamp convertTimestamp(Value value, java.util.Calendar calendar)
          Convert the timestamp to the specified time zone.
static long convertToLocal(java.util.Date x, java.util.Calendar target)
          Convert a date to the specified time zone.
static long dateValue(long year, int month, int day)
          Get the date value from a given date.
static long dateValueFromAbsoluteDay(long absoluteDay)
          Calculate the date value from an absolute day.
static long dateValueFromDate(long ms)
          Calculate the date value (in the default timezone) from a given time in milliseconds in UTC.
static int dayFromDateValue(long x)
          Get the day of month from a date value.
static java.lang.String formatDateTime(java.util.Date date, java.lang.String format, java.lang.String locale, java.lang.String timeZone)
          Formats a date using a format string.
static int getDatePart(java.util.Date d, int field)
          Get the specified field of a date, however with years normalized to positive or negative, and month starting with 1.
static int getIsoDayOfWeek(java.util.Date date)
          Return the day of week according to the ISO 8601 specification.
static int getIsoWeek(java.util.Date date)
          Returns the week of the year according to the ISO 8601 specification.
static int getIsoYear(java.util.Date date)
          Returns the year according to the ISO week definition.
static long getMillis(java.util.TimeZone tz, int year, int month, int day, int hour, int minute, int second, int millis)
          Calculate the milliseconds since 1970-01-01 (UTC) for the given date and time (in the specified timezone).
static long getTimeLocalWithoutDst(java.util.Date d)
          Get the number of milliseconds since 1970-01-01 in the local timezone, but without daylight saving time into account.
static long getTimeUTCWithoutDst(long millis)
          Convert the number of milliseconds since 1970-01-01 in the local timezone to UTC, but without daylight saving time into account.
static boolean isValidDate(int year, int month, int day)
          Verify if the specified date is valid.
static int monthFromDateValue(long x)
          Get the month from a date value.
static long nanosFromDate(long ms)
          Calculate the nanoseconds since midnight (in the default timezone) from a given time in milliseconds in UTC.
static ValueTimestamp normalizeTimestamp(long absoluteDay, long nanos)
          Calculate the normalized timestamp.
static java.util.Date parseDateTime(java.lang.String date, java.lang.String format, java.lang.String locale, java.lang.String timeZone)
          Parses a date using a format string.
static long parseDateValue(java.lang.String s, int start, int end)
          Parse a date string.
static long parseTimeNanos(java.lang.String s, int start, int end, boolean timeOfDay)
          Parse a time string.
static void resetCalendar()
          Reset the calendar, for example after changing the default timezone.
static int yearFromDateValue(long x)
          Get the year from a date value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MILLIS_PER_DAY

public static final long MILLIS_PER_DAY
The number of milliseconds per day.

See Also:
Constant Field Values
Method Detail

resetCalendar

public static void resetCalendar()
Reset the calendar, for example after changing the default timezone.


convertDate

public static java.sql.Date convertDate(Value value,
                                        java.util.Calendar calendar)
Convert the date to the specified time zone.

Parameters:
value - the date (might be ValueNull)
calendar - the calendar
Returns:
the date using the correct time zone

convertTime

public static java.sql.Time convertTime(Value value,
                                        java.util.Calendar calendar)
Convert the time to the specified time zone.

Parameters:
value - the time (might be ValueNull)
calendar - the calendar
Returns:
the time using the correct time zone

convertTimestamp

public static java.sql.Timestamp convertTimestamp(Value value,
                                                  java.util.Calendar calendar)
Convert the timestamp to the specified time zone.

Parameters:
value - the timestamp (might be ValueNull)
calendar - the calendar
Returns:
the timestamp using the correct time zone

convertDate

public static ValueDate convertDate(java.sql.Date x,
                                    java.util.Calendar calendar)
Convert the date using the specified calendar.

Parameters:
x - the date
calendar - the calendar
Returns:
the date

convertTime

public static ValueTime convertTime(java.sql.Time x,
                                    java.util.Calendar calendar)
Convert the time using the specified calendar.

Parameters:
x - the time
calendar - the calendar
Returns:
the time

convertToLocal

public static long convertToLocal(java.util.Date x,
                                  java.util.Calendar target)
Convert a date to the specified time zone.

Parameters:
x - the date to convert
target - the calendar with the target timezone
Returns:
the milliseconds the milliseconds in UTC

convertTimestamp

public static ValueTimestamp convertTimestamp(java.sql.Timestamp x,
                                              java.util.Calendar calendar)
Convert the timestamp using the specified calendar.

Parameters:
x - the time
calendar - the calendar
Returns:
the timestamp

parseDateValue

public static long parseDateValue(java.lang.String s,
                                  int start,
                                  int end)
Parse a date string. The format is: [+|-]year-month-day

Parameters:
s - the string to parse
start - the parse index start
end - the parse index end
Returns:
the date value
Throws:
java.lang.IllegalArgumentException - if there is a problem

parseTimeNanos

public static long parseTimeNanos(java.lang.String s,
                                  int start,
                                  int end,
                                  boolean timeOfDay)
Parse a time string. The format is: [-]hour:minute:second[.nanos]

Parameters:
s - the string to parse
start - the parse index start
end - the parse index end
timeOfDay - whether the result need to be within 0 (inclusive) and 1 day (exclusive)
Returns:
the time in nanoseconds
Throws:
java.lang.IllegalArgumentException - if there is a problem

getMillis

public static long getMillis(java.util.TimeZone tz,
                             int year,
                             int month,
                             int day,
                             int hour,
                             int minute,
                             int second,
                             int millis)
Calculate the milliseconds since 1970-01-01 (UTC) for the given date and time (in the specified timezone).

Parameters:
tz - the timezone of the parameters
year - the absolute year (positive or negative)
month - the month (1-12)
day - the day (1-31)
hour - the hour (0-23)
minute - the minutes (0-59)
second - the number of seconds (0-59)
millis - the number of milliseconds
Returns:
the number of milliseconds (UTC)

getDatePart

public static int getDatePart(java.util.Date d,
                              int field)
Get the specified field of a date, however with years normalized to positive or negative, and month starting with 1.

Parameters:
d - the date
field - the field type
Returns:
the value

getTimeLocalWithoutDst

public static long getTimeLocalWithoutDst(java.util.Date d)
Get the number of milliseconds since 1970-01-01 in the local timezone, but without daylight saving time into account.

Parameters:
d - the date
Returns:
the milliseconds

getTimeUTCWithoutDst

public static long getTimeUTCWithoutDst(long millis)
Convert the number of milliseconds since 1970-01-01 in the local timezone to UTC, but without daylight saving time into account.

Parameters:
millis - the number of milliseconds in the local timezone
Returns:
the number of milliseconds in UTC

getIsoDayOfWeek

public static int getIsoDayOfWeek(java.util.Date date)
Return the day of week according to the ISO 8601 specification. Week starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601

Parameters:
date - the date object which day of week should be calculated
Returns:
the day of the week, Monday as 1 to Sunday as 7

getIsoWeek

public static int getIsoWeek(java.util.Date date)
Returns the week of the year according to the ISO 8601 specification. The spec defines the first week of the year as the week which contains at least 4 days of the new year. The week starts at Monday. Therefore December 29th - 31th could belong to the next year and January 1st - 3th could belong to the previous year. If January 1st is on Thursday (or earlier) it belongs to the first week, otherwise to the last week of the previous year. Hence January 4th always belongs to the first week while the December 28th always belongs to the last week.

Parameters:
date - the date object which week of year should be calculated
Returns:
the week of the year

getIsoYear

public static int getIsoYear(java.util.Date date)
Returns the year according to the ISO week definition.

Parameters:
date - the date object which year should be calculated
Returns:
the year

formatDateTime

public static java.lang.String formatDateTime(java.util.Date date,
                                              java.lang.String format,
                                              java.lang.String locale,
                                              java.lang.String timeZone)
Formats a date using a format string.

Parameters:
date - the date to format
format - the format string
locale - the locale
timeZone - the timezone
Returns:
the formatted date

parseDateTime

public static java.util.Date parseDateTime(java.lang.String date,
                                           java.lang.String format,
                                           java.lang.String locale,
                                           java.lang.String timeZone)
Parses a date using a format string.

Parameters:
date - the date to parse
format - the parsing format
locale - the locale
timeZone - the timeZone
Returns:
the parsed date

isValidDate

public static boolean isValidDate(int year,
                                  int month,
                                  int day)
Verify if the specified date is valid.

Parameters:
year - the year
month - the month (January is 1)
day - the day (1 is the first of the month)
Returns:
true if it is valid

convertDateValueToDate

public static java.sql.Date convertDateValueToDate(long dateValue)
Convert a date value to a date, using the default timezone.

Parameters:
dateValue - the date value
Returns:
the date

convertDateValueToTimestamp

public static java.sql.Timestamp convertDateValueToTimestamp(long dateValue,
                                                             long nanos)
Convert a date value / time value to a timestamp, using the default timezone.

Parameters:
dateValue - the date value
nanos - the nanoseconds since midnight
Returns:
the timestamp

convertNanoToTime

public static java.sql.Time convertNanoToTime(long nanos)
Convert a time value to a time, using the default timezone.

Parameters:
nanos - the nanoseconds since midnight
Returns:
the time

yearFromDateValue

public static int yearFromDateValue(long x)
Get the year from a date value.

Parameters:
x - the date value
Returns:
the year

monthFromDateValue

public static int monthFromDateValue(long x)
Get the month from a date value.

Parameters:
x - the date value
Returns:
the month (1..12)

dayFromDateValue

public static int dayFromDateValue(long x)
Get the day of month from a date value.

Parameters:
x - the date value
Returns:
the day (1..31)

dateValue

public static long dateValue(long year,
                             int month,
                             int day)
Get the date value from a given date.

Parameters:
year - the year
month - the month (1..12)
day - the day (1..31)
Returns:
the date value

dateValueFromDate

public static long dateValueFromDate(long ms)
Calculate the date value (in the default timezone) from a given time in milliseconds in UTC.

Parameters:
ms - the milliseconds
Returns:
the date value

nanosFromDate

public static long nanosFromDate(long ms)
Calculate the nanoseconds since midnight (in the default timezone) from a given time in milliseconds in UTC.

Parameters:
ms - the milliseconds
Returns:
the nanoseconds

normalizeTimestamp

public static ValueTimestamp normalizeTimestamp(long absoluteDay,
                                                long nanos)
Calculate the normalized timestamp.

Parameters:
absoluteDay - the absolute day
nanos - the nanoseconds (may be negative or larger than one day)
Returns:
the timestamp

absoluteDayFromDateValue

public static long absoluteDayFromDateValue(long dateValue)
Calculate the absolute day from a date value.

Parameters:
dateValue - the date value
Returns:
the absolute day

dateValueFromAbsoluteDay

public static long dateValueFromAbsoluteDay(long absoluteDay)
Calculate the date value from an absolute day.

Parameters:
absoluteDay - the absolute day
Returns:
the date value