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.


Method Summary
static java.sql.Date cloneAndNormalizeDate(java.sql.Date value)
          Clone a date object and reset the hour, minutes, seconds, and milliseconds to zero.
static java.sql.Time cloneAndNormalizeTime(java.sql.Time value)
          Clone a time object and reset the day to 1970-01-01.
static java.sql.Date convertDateToCalendar(java.sql.Date x, java.util.Calendar calendar)
          Convert the date to the specified time zone.
static Value convertDateToUniversal(java.sql.Date x, java.util.Calendar source)
          Convert the date from the specified time zone to UTC.
static java.sql.Timestamp convertTimestampToCalendar(java.sql.Timestamp x, java.util.Calendar calendar)
          Convert the timestamp to the specified time zone.
static Value convertTimestampToUniversal(java.sql.Timestamp x, java.util.Calendar source)
          Convert the timestamp from the specified time zone to UTC.
static java.sql.Time convertTimeToCalendar(java.sql.Time x, java.util.Calendar calendar)
          Convert the time to the specified time zone.
static Value convertTimeToUniversal(java.sql.Time x, java.util.Calendar source)
          Convert the time from the specified time zone to UTC.
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 getTimeGMT(long millis)
          Convert the number of milliseconds since 1970-01-01 in the local timezone to GMT.
static long getTimeLocal(java.util.Date d)
          Get the number of milliseconds since 1970-01-01 in the local timezone.
static java.util.Date parseDateTime(java.lang.String original, int type, int errorCode)
          Parse a date, time or timestamp value.
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 void resetCalendar()
          Reset the calendar, for example after changing the default timezone.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

resetCalendar

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


convertTimestampToCalendar

public static java.sql.Timestamp convertTimestampToCalendar(java.sql.Timestamp x,
                                                            java.util.Calendar calendar)
Convert the timestamp to the specified time zone.

Parameters:
x - the timestamp
calendar - the calendar
Returns:
the timestamp using the correct time zone

cloneAndNormalizeTime

public static java.sql.Time cloneAndNormalizeTime(java.sql.Time value)
Clone a time object and reset the day to 1970-01-01.

Parameters:
value - the time value
Returns:
the time value without the date component

cloneAndNormalizeDate

public static java.sql.Date cloneAndNormalizeDate(java.sql.Date value)
Clone a date object and reset the hour, minutes, seconds, and milliseconds to zero.

Parameters:
value - the date value
Returns:
the date value at midnight

convertDateToUniversal

public static Value convertDateToUniversal(java.sql.Date x,
                                           java.util.Calendar source)
Convert the date from the specified time zone to UTC.

Parameters:
x - the date
source - the calendar
Returns:
the date in UTC

convertTimeToUniversal

public static Value convertTimeToUniversal(java.sql.Time x,
                                           java.util.Calendar source)
Convert the time from the specified time zone to UTC.

Parameters:
x - the time
source - the calendar
Returns:
the time in UTC

convertTimestampToUniversal

public static Value convertTimestampToUniversal(java.sql.Timestamp x,
                                                java.util.Calendar source)
Convert the timestamp from the specified time zone to UTC.

Parameters:
x - the time
source - the calendar
Returns:
the timestamp in UTC

convertDateToCalendar

public static java.sql.Date convertDateToCalendar(java.sql.Date x,
                                                  java.util.Calendar calendar)
Convert the date to the specified time zone.

Parameters:
x - the date
calendar - the calendar
Returns:
the date using the correct time zone

convertTimeToCalendar

public static java.sql.Time convertTimeToCalendar(java.sql.Time x,
                                                  java.util.Calendar calendar)
Convert the time to the specified time zone.

Parameters:
x - the time
calendar - the calendar
Returns:
the time using the correct time zone

parseDateTime

public static java.util.Date parseDateTime(java.lang.String original,
                                           int type,
                                           int errorCode)
Parse a date, time or timestamp value. This method supports the format +/-year-month-day hour:minute:seconds.fractional and an optional timezone part.

Parameters:
original - the original string
type - the value type (Value.TIME, TIMESTAMP, or DATE)
errorCode - the error code to use if an error occurs
Returns:
the date object

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

getTimeLocal

public static long getTimeLocal(java.util.Date d)
Get the number of milliseconds since 1970-01-01 in the local timezone.

Parameters:
d - the date
Returns:
the milliseconds

getTimeGMT

public static long getTimeGMT(long millis)
Convert the number of milliseconds since 1970-01-01 in the local timezone to GMT.

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

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