DateTimeUtils

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).

Methods
static long absoluteDayFromDateValue(long dateValue)
Calculate the absolute day from an encoded date value.
static long absoluteDayFromDateValue(long dateValue)
Calculate the absolute day from an encoded date value.
Parameters:
dateValue - the date value
Returns:
the absolute day
static long absoluteDayFromYear(long year)
Calculate the absolute day for a January, 1 of the specified year.
static long absoluteDayFromYear(long year)
Calculate the absolute day for a January, 1 of the specified year.
Parameters:
year - the year
Returns:
the absolute day
static void appendDate(StringBuilder buff, long dateValue)
Append a date to the string builder.
static void appendDate(StringBuilder buff, long dateValue)
Append a date to the string builder.
Parameters:
buff - the target string builder
dateValue - the date value
static void appendTime(StringBuilder buff, long nanos)
Append a time to the string builder.
static void appendTime(StringBuilder buff, long nanos)
Append a time to the string builder.
Parameters:
buff - the target string builder
nanos - the time in nanoseconds
static void appendTimeZone(StringBuilder buff, short tz)
Append a time zone to the string builder.
static void appendTimeZone(StringBuilder buff, short tz)
Append a time zone to the string builder.
Parameters:
buff - the target string builder
tz - the time zone in minutes
static Date convertDate(Value value, Calendar calendar)
Convert the date to the specified time zone.
static Date convertDate(Value value, 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
static ValueDate convertDate(Date x, Calendar calendar)
Convert a java.util.Date using the specified calendar.
static ValueDate convertDate(Date x, Calendar calendar)
Convert a java.util.Date using the specified calendar.
Parameters:
x - the date
calendar - the calendar
Returns:
the date
static long convertDateTimeValueToMillis(TimeZone tz, long dateValue, long ms)
Convert an encoded date-time value to millis, using the supplied timezone.
static long convertDateTimeValueToMillis(TimeZone tz, long dateValue, long ms)
Convert an encoded date-time value to millis, using the supplied timezone.
Parameters:
tz - the timezone
dateValue - the date value
ms - milliseconds of day
Returns:
the date
static Date convertDateValueToDate(long dateValue)
Convert an encoded date value to a java.util.Date, using the default timezone.
static Date convertDateValueToDate(long dateValue)
Convert an encoded date value to a java.util.Date, using the default timezone.
Parameters:
dateValue - the date value
Returns:
the date
static Timestamp convertDateValueToTimestamp(long dateValue, long timeNanos)
Convert an encoded date value / time value to a timestamp, using the default timezone.
static Timestamp convertDateValueToTimestamp(long dateValue, long timeNanos)
Convert an encoded date value / time value to a timestamp, using the default timezone.
Parameters:
dateValue - the date value
timeNanos - the nanoseconds since midnight
Returns:
the timestamp
static Time convertNanoToTime(long nanosSinceMidnight)
Convert a time value to a time, using the default timezone.
static Time convertNanoToTime(long nanosSinceMidnight)
Convert a time value to a time, using the default timezone.
Parameters:
nanosSinceMidnight - the nanoseconds since midnight
Returns:
the time
static long convertScale(long nanosOfDay, int scale)
Converts scale of nanoseconds.
static long convertScale(long nanosOfDay, int scale)
Converts scale of nanoseconds.
Parameters:
nanosOfDay - nanoseconds of day
scale - fractional seconds precision
Returns:
scaled value
static Time convertTime(Value value, Calendar calendar)
Convert the time to the specified time zone.
static Time convertTime(Value value, 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
static ValueTime convertTime(Time x, Calendar calendar)
Convert the time using the specified calendar.
static ValueTime convertTime(Time x, Calendar calendar)
Convert the time using the specified calendar.
Parameters:
x - the time
calendar - the calendar
Returns:
the time
static Timestamp convertTimestamp(Value value, Calendar calendar)
Convert the timestamp to the specified time zone.
static Timestamp convertTimestamp(Value value, 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
static ValueTimestamp convertTimestamp(Timestamp x, Calendar calendar)
Convert the timestamp using the specified calendar.
static ValueTimestamp convertTimestamp(Timestamp x, Calendar calendar)
Convert the timestamp using the specified calendar.
Parameters:
x - the time
calendar - the calendar
Returns:
the timestamp
static Timestamp convertTimestampTimeZoneToTimestamp(long dateValue, long timeNanos, short offsetMins)
Convert an encoded date value / time value to a timestamp using the specified time zone offset.
static Timestamp convertTimestampTimeZoneToTimestamp(long dateValue, long timeNanos, short offsetMins)
Convert an encoded date value / time value to a timestamp using the specified time zone offset.
Parameters:
dateValue - the date value
timeNanos - the nanoseconds since midnight
offsetMins - time zone offset in minutes
Returns:
the timestamp
static GregorianCalendar createGregorianCalendar()
Creates a Gregorian calendar for the default timezone using the default locale.
static GregorianCalendar createGregorianCalendar()
Creates a Gregorian calendar for the default timezone using the default locale. Dates in H2 are represented in a Gregorian calendar. So this method should be used instead of Calendar.getInstance() to ensure that the Gregorian calendar is used for all date processing instead of a default locale calendar that can be non-Gregorian in some locales.
Returns:
a new calendar instance.
static GregorianCalendar createGregorianCalendar(TimeZone tz)
Creates a Gregorian calendar for the given timezone using the default locale.
static GregorianCalendar createGregorianCalendar(TimeZone tz)
Creates a Gregorian calendar for the given timezone using the default locale. Dates in H2 are represented in a Gregorian calendar. So this method should be used instead of Calendar.getInstance() to ensure that the Gregorian calendar is used for all date processing instead of a default locale calendar that can be non-Gregorian in some locales.
Parameters:
tz - timezone for the calendar, is never null
Returns:
a new calendar instance.
static long[] dateAndTimeFromValue(Value value)
Extracts date value and nanos of day from the specified value.
static long[] dateAndTimeFromValue(Value value)
Extracts date value and nanos of day from the specified value.
Parameters:
value - value to extract fields from
Returns:
array with date value and nanos of day
static Value dateTimeToValue(Value original, long dateValue, long timeNanos, boolean forceTimestamp)
Creates a new date-time value with the same type as original value.
static Value dateTimeToValue(Value original, long dateValue, long timeNanos, boolean forceTimestamp)
Creates a new date-time value with the same type as original value. If original value is a ValueTimestampTimeZone, returned value will have the same time zone offset as original value.
Parameters:
original - original value
dateValue - date value for the returned value
timeNanos - nanos of day for the returned value
forceTimestamp - if {@code true} return ValueTimestamp if original argument is ValueDate or ValueTime
Returns:
new value with specified date value and nanos of day
static long dateValue(long year, int month, int day)
Get the date value from a given date.
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
static long dateValueFromAbsoluteDay(long absoluteDay)
Calculate the encoded date value from an absolute day.
static long dateValueFromAbsoluteDay(long absoluteDay)
Calculate the encoded date value from an absolute day.
Parameters:
absoluteDay - the absolute day
Returns:
the date value
static long dateValueFromDate(long ms)
Convert a UTC datetime in millis to an encoded date in the default timezone.
static long dateValueFromDate(long ms)
Convert a UTC datetime in millis to an encoded date in the default timezone.
Parameters:
ms - the milliseconds
Returns:
the date value
static long dateValueFromDenormalizedDate(long year, long month, int day)
Get the date value from a given denormalized date with possible out of range values of month and/or day.
static long dateValueFromDenormalizedDate(long year, long month, int day)
Get the date value from a given denormalized date with possible out of range values of month and/or day. Used after addition or subtraction month or years to (from) it to get a valid date.
Parameters:
year - the year
month - the month, if out of range month and year will be normalized
day - the day of the month, if out of range it will be saturated
Returns:
the date value
static int dayFromDateValue(long x)
Get the day of month from a date value.
static int dayFromDateValue(long x)
Get the day of month from a date value.
Parameters:
x - the date value
Returns:
the day (1..31)
static long decrementDateValue(long dateValue)
Return the previous date value.
static long decrementDateValue(long dateValue)
Return the previous date value.
Parameters:
dateValue - the date value
Returns:
the previous date value
static GregorianCalendar getCalendar()
Get a calendar for the default timezone.
static GregorianCalendar getCalendar()
Get a calendar for the default timezone.
Returns:
a calendar instance. A cached instance is returned where possible
static int getDayOfWeek(long dateValue, int firstDayOfWeek)
Returns day of week.
static int getDayOfWeek(long dateValue, int firstDayOfWeek)
Returns day of week.
Parameters:
dateValue - the date value
firstDayOfWeek - first day of week, Monday as 1, Sunday as 7 or 0
Returns:
day of week
static int getDayOfWeekFromAbsolute(long absoluteValue, int firstDayOfWeek)
Get the day of the week from the absolute day value.
static int getDayOfWeekFromAbsolute(long absoluteValue, int firstDayOfWeek)
Get the day of the week from the absolute day value.
Parameters:
absoluteValue - the absolute day
firstDayOfWeek - the first day of the week
Returns:
the day of week
static int getDayOfYear(long dateValue)
Returns number of day in year.
static int getDayOfYear(long dateValue)
Returns number of day in year.
Parameters:
dateValue - the date value
Returns:
number of day in year
static int getDaysInMonth(int year, int month)
Returns number of days in month.
static int getDaysInMonth(int year, int month)
Returns number of days in month.
Parameters:
year - the year
month - the month
Returns:
number of days in the specified month
static int getIsoDayOfWeek(long dateValue)
Returns ISO day of week.
static int getIsoDayOfWeek(long dateValue)
Returns ISO day of week.
Parameters:
dateValue - the date value
Returns:
ISO day of week, Monday as 1 to Sunday as 7
static int getIsoWeekOfYear(long dateValue)
Returns ISO number of week in year.
static int getIsoWeekOfYear(long dateValue)
Returns ISO number of week in year.
Parameters:
dateValue - the date value
Returns:
number of week in year
static int getIsoWeekYear(long dateValue)
Returns ISO week year.
static int getIsoWeekYear(long dateValue)
Returns ISO week year.
Parameters:
dateValue - the date value
Returns:
ISO week year
static long getMillis(long dateValue, long timeNanos, short offsetMins)
Calculates the milliseconds since epoch for the specified date value, nanoseconds since midnight, and time zone offset.
static long getMillis(long dateValue, long timeNanos, short offsetMins)
Calculates the milliseconds since epoch for the specified date value, nanoseconds since midnight, and time zone offset.
Parameters:
dateValue - date value
timeNanos - nanoseconds since midnight
offsetMins - time zone offset in minutes
Returns:
milliseconds since epoch in UTC
static long getMillis(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 getMillis(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, or null for the default timezone
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)
static int getSundayDayOfWeek(long dateValue)
Returns day of week with Sunday as 1.
static int getSundayDayOfWeek(long dateValue)
Returns day of week with Sunday as 1.
Parameters:
dateValue - the date value
Returns:
day of week, Sunday as 1 to Monday as 7
static long getTimeLocalWithoutDst(Date d)
Get the number of milliseconds since 1970-01-01 in the local timezone, but without daylight saving time into account.
static long getTimeLocalWithoutDst(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
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 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
static int getTimeZoneOffsetMillis(TimeZone tz, long dateValue, long timeNanos)
Calculates the time zone offset in minutes for the specified time zone, date value, and nanoseconds since midnight.
static int getTimeZoneOffsetMillis(TimeZone tz, long dateValue, long timeNanos)
Calculates the time zone offset in minutes for the specified time zone, date value, and nanoseconds since midnight.
Parameters:
tz - time zone, or {@code null} for default
dateValue - date value
timeNanos - nanoseconds since midnight
Returns:
time zone offset in milliseconds
static int getWeekOfYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek)
Returns number of week in year.
static int getWeekOfYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek)
Returns number of week in year.
Parameters:
dateValue - the date value
firstDayOfWeek - first day of week, Monday as 1, Sunday as 7 or 0
minimalDaysInFirstWeek - minimal days in first week of year
Returns:
number of week in year
static int getWeekYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek)
Returns week year.
static int getWeekYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek)
Returns week year.
Parameters:
dateValue - the date value
firstDayOfWeek - first day of week, Monday as 1, Sunday as 7 or 0
minimalDaysInFirstWeek - minimal days in first week of year
Returns:
week year
static long incrementDateValue(long dateValue)
Return the next date value.
static long incrementDateValue(long dateValue)
Return the next date value.
Parameters:
dateValue - the date value
Returns:
the next date value
static boolean isValidDate(int year, int month, int day)
Verify if the specified date is valid.
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
static int monthFromDateValue(long x)
Get the month from a date value.
static int monthFromDateValue(long x)
Get the month from a date value.
Parameters:
x - the date value
Returns:
the month (1..12)
static long nanosFromDate(long ms)
Convert a time in milliseconds in UTC to the nanoseconds since midnight (in the default timezone).
static long nanosFromDate(long ms)
Convert a time in milliseconds in UTC to the nanoseconds since midnight (in the default timezone).
Parameters:
ms - the milliseconds
Returns:
the nanoseconds
static ValueTimestamp normalizeTimestamp(long absoluteDay, long nanos)
Calculate the normalized timestamp.
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
static long parseDateValue(String s, int start, int end)
Parse a date string.
static long parseDateValue(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:
IllegalArgumentException - if there is a problem
static long parseTimeNanos(String s, int start, int end, boolean timeOfDay)
Parse a time string.
static long parseTimeNanos(String s, int start, int end, boolean timeOfDay)
Parse a time string. The format is: [-]hour:minute:second[.nanos] or alternatively [-]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:
IllegalArgumentException - if there is a problem
static Value parseTimestamp(String s, Mode mode, boolean withTimeZone)
Parses timestamp value from the specified string.
static Value parseTimestamp(String s, Mode mode, boolean withTimeZone)
Parses timestamp value from the specified string.
Parameters:
s - string to parse
mode - database mode, or {@code null}
withTimeZone - if {@code true} return {@link ValueTimestampTimeZone} instead of {@link ValueTimestamp}
Returns:
parsed timestamp
static long prolepticGregorianAbsoluteDayFromDateValue(long dateValue)
Calculate the absolute day from an encoded date value in proleptic Gregorian calendar.
static long prolepticGregorianAbsoluteDayFromDateValue(long dateValue)
Calculate the absolute day from an encoded date value in proleptic Gregorian calendar.
Parameters:
dateValue - the date value
Returns:
the absolute day in proleptic Gregorian calendar
static void resetCalendar()
Reset the cached calendar for default timezone, for example after changing the default timezone.
static void resetCalendar()
Reset the cached calendar for default timezone, for example after changing the default timezone.
static String timeZoneNameFromOffsetMins(int offsetMins)
Generates time zone name for the specified offset in minutes.
static String timeZoneNameFromOffsetMins(int offsetMins)
Generates time zone name for the specified offset in minutes.
Parameters:
offsetMins - offset in minutes
Returns:
time zone name
static ValueTimestampTimeZone timestampTimeZoneFromLocalDateValueAndNanos(long dateValue, long timeNanos)
Converts local date value and nanoseconds to timestamp with time zone.
static ValueTimestampTimeZone timestampTimeZoneFromLocalDateValueAndNanos(long dateValue, long timeNanos)
Converts local date value and nanoseconds to timestamp with time zone.
Parameters:
dateValue - date value
timeNanos - nanoseconds since midnight
Returns:
timestamp with time zone
static String timestampTimeZoneToString(long dateValue, long timeNanos, short timeZoneOffsetMins)
Formats timestamp with time zone as string.
static String timestampTimeZoneToString(long dateValue, long timeNanos, short timeZoneOffsetMins)
Formats timestamp with time zone as string.
Parameters:
dateValue - the year-month-day bit field
timeNanos - nanoseconds since midnight
timeZoneOffsetMins - the time zone offset in minutes
Returns:
formatted string
static int yearFromDateValue(long x)
Get the year from a date value.
static int yearFromDateValue(long x)
Get the year from a date value.
Parameters:
x - the date value
Returns:
the year

Fields
static int EPOCH_DATE_VALUE = 1008673
static long MILLIS_PER_DAY = 86400000L
static long NANOS_PER_DAY = 86400000000000L
static long SECONDS_PER_DAY = 86400L
static TimeZone UTC

EPOCH_DATE_VALUE = 1008673

Date value for 1970-01-01.

NANOS_PER_DAY = 86400000000000L

The number of nanoseconds per day.

MILLIS_PER_DAY = 86400000L

The number of milliseconds per day.

SECONDS_PER_DAY = 86400L

The number of seconds per day.

UTC

UTC time zone.