Top Description Fields Constructors Methods
org.h2.util

public Class DateTimeUtils

extends Object
Class Inheritance
Imports
java.time.Instant, org.h2.api.ErrorCode, org.h2.engine.CastDataProvider, org.h2.message.DbException, org.h2.value.TypeInfo, .Value, .ValueDate, .ValueTime, .ValueTimeTimeZone, .ValueTimestamp, .ValueTimestampTimeZone

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

Modifier and TypeField and Description
public static final int
EPOCH_DATE_VALUE

Date value for 1970-01-01.

pack-priv static final int[]
private static volatile TimeZoneProvider
public static final long
MAX_DATE_VALUE

Maximum possible date value.

public static final long
MILLIS_PER_DAY

The number of milliseconds per day.

public static final long
MIN_DATE_VALUE

Minimum possible date value.

public static final long
NANOS_PER_DAY

The number of nanoseconds per day.

public static final long
NANOS_PER_HOUR

The number of nanoseconds per hour.

public static final long
NANOS_PER_MINUTE

The number of nanoseconds per minute.

public static final long
NANOS_PER_SECOND

The number of nanoseconds per second.

private static final int[]
public static final long
SECONDS_PER_DAY

The number of seconds per day.

public static final int
SHIFT_MONTH

The offset of month bits in date values.

public static final int
SHIFT_YEAR

The offset of year bits in date values.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static long

Returns:

the absolute day
absoluteDay
(long
year
y
,
int
month
m
,
int
day
d
)

Calculate the absolute day.

public static long

Returns:

the absolute day
absoluteDayFromDateValue
(long
the date value
dateValue
)

Calculate the absolute day from an encoded date value.

public static long

Returns:

the absolute day
absoluteDayFromYear
(long
the year
year
)

Calculate the absolute day for a January, 1 of the specified year.

public static StringBuilder

Returns:

the specified string builder
appendDate
(StringBuilder
the target string builder
builder
,
long
the date value
dateValue
)

Append a date to the string builder.

pack-priv static StringBuilder

Returns:

the specified string builder
appendNanos
(StringBuilder
string builder to append to
builder
,
int
nanoseconds of second
nanos
)

Append nanoseconds of time, if any.

public static StringBuilder

Returns:

the specified string builder
appendTime
(StringBuilder
the target string builder
builder
,
long
the time in nanoseconds
nanos
)

Append a time to the string builder.

public static StringBuilder

Returns:

the specified string builder
appendTimeZone
(StringBuilder
the target string builder
builder
,
int
the time zone offset in seconds
tz
)

Append a time zone to the string builder.

public static long

Returns:

scaled value
convertScale
(long
nanoseconds of day
nanosOfDay
,
int
fractional seconds precision
scale
,
long
the allowed range of values (0..range-1)
range
)

Converts scale of nanoseconds.

public static ValueTimestampTimeZone

Returns:

current timestamp
currentTimestamp
(TimeZoneProvider
the time zone
timeZone
)

Returns current timestamp.

public static ValueTimestampTimeZone

Returns:

current timestamp
currentTimestamp
(TimeZoneProvider
the time zone
timeZone
,
Instant
timestamp source, must be greater than or equal to 1970-01-01T00:00:00Z
now
)

Returns current timestamp using the specified instant for its value.

public static long[]

Returns:

array with date value and nanos of day
dateAndTimeFromValue
(Value
value to extract fields from
value
,
CastDataProvider
the cast information provider
provider
)

Extracts date value and nanos of day from the specified value.

public static Value

Returns:

new value with specified date value and nanos of day
dateTimeToValue
(Value
original value
original
,
long
date value for the returned value
dateValue
,
long
nanos of day for the returned value
timeNanos
)

Creates a new date-time value with the same type as original value.

public static long

Returns:

the date value
dateValue
(long
the year
year
,
int
the month (1..12)
month
,
int
the day (1..31)
day
)

Get the date value from a given date.

public static long

Returns:

the date value
dateValueFromAbsoluteDay
(long
the absolute day
absoluteDay
)

Calculate the encoded date value from an absolute day.

public static long

Returns:

the date value
dateValueFromDenormalizedDate
(long
the year
year
,
long
the month, if out of range month and year will be normalized
month
,
int
the day of the month, if out of range it will be saturated
day
)

Get the date value from a given denormalized date with possible out of range values of month and/or day.

public static long

Returns:

the date value
dateValueFromLocalSeconds
(long
the seconds since 1970-01-01
localSeconds
)

Convert a local seconds to an encoded date.

public static int

Returns:

the day (1..31)
dayFromDateValue
(long
the date value
x
)

Get the day of month from a date value.

public static long

Returns:

the previous date value
decrementDateValue
(long
the date value
dateValue
)

Return the previous date value.

public static int

Returns:

day of week
getDayOfWeek
(long
the date value
dateValue
,
int
first day of week, Monday as 1, Sunday as 7 or 0
firstDayOfWeek
)

Returns day of week.

public static int

Returns:

the day of week
getDayOfWeekFromAbsolute
(long
the absolute day
absoluteValue
,
int
the first day of the week
firstDayOfWeek
)

Get the day of the week from the absolute day value.

public static int

Returns:

number of day in year
getDayOfYear
(long
the date value
dateValue
)

Returns number of day in year.

public static int

Returns:

number of days in the specified month
getDaysInMonth
(int
the year
year
,
int
the month
month
)

Returns number of days in month.

public static long

Returns:

seconds since epoch in UTC
getEpochSeconds
(long
date value
dateValue
,
long
nanoseconds since midnight
timeNanos
,
int
time zone offset in seconds
offsetSeconds
)

Calculates the seconds since epoch for the specified date value, nanoseconds since midnight, and time zone offset.

public static int

Returns:

ISO day of week, Monday as 1 to Sunday as 7
getIsoDayOfWeek
(long
the date value
dateValue
)

Returns ISO day of week.

public static int

Returns:

number of week in year
getIsoWeekOfYear
(long
the date value
dateValue
)

Returns ISO number of week in year.

public static int

Returns:

ISO week year
getIsoWeekYear
(long
the date value
dateValue
)

Returns ISO week year.

public static int

Returns:

day of week, Sunday as 1 to Monday as 7
getSundayDayOfWeek
(long
the date value
dateValue
)

Returns day of week with Sunday as 1.

public static TimeZoneProvider

Returns:

the time zone provider for the default time zone
getTimeZone
()

Get the time zone provider for the default time zone.

public static int

Returns:

number of week in year
getWeekOfYear
(long
the date value
dateValue
,
int
first day of week, Monday as 1, Sunday as 7 or 0
firstDayOfWeek
,
int
minimal days in first week of year
minimalDaysInFirstWeek
)

Returns number of week in year.

public static int

Returns:

week year
getWeekYear
(long
the date value
dateValue
,
int
first day of week, Monday as 1, Sunday as 7 or 0
firstDayOfWeek
,
int
minimal days in first week of year
minimalDaysInFirstWeek
)

Returns week year.

public static long

Returns:

absolute day of the first day in the week year
getWeekYearAbsoluteStart
(int
the week year
weekYear
,
int
first day of week, Monday as 1, Sunday as 7 or 0
firstDayOfWeek
,
int
minimal days in first week of year
minimalDaysInFirstWeek
)

Get absolute day of the first day in the week year.

public static long

Returns:

the next date value
incrementDateValue
(long
the date value
dateValue
)

Return the next date value.

pack-priv static boolean
isLeapYear(int year)

public static boolean

Returns:

true if it is valid
isValidDate
(int
the year
year
,
int
the month (January is 1)
month
,
int
the day (1 is the first of the month)
day
)

Verify if the specified date is valid.

public static int

Returns:

the month (1..12)
monthFromDateValue
(long
the date value
x
)

Get the month from a date value.

public static long

Returns:

the nanoseconds
nanosFromLocalSeconds
(long
the seconds since 1970-01-01
localSeconds
)

Convert a time in seconds in local time to the nanoseconds since midnight.

public static long

Returns:

the nanos of day within a day
normalizeNanosOfDay
(long
the nanoseconds (may be negative or larger than one day)
nanos
)

Calculate the normalized nanos of day.

public static long

Returns:

the date value
parseDateValue
(String
the string to parse
s
,
int
the parse index start
start
,
int
the parse index end
end
)

Parse a date string.

pack-priv static int

Returns:

Parsed nanoseconds.
parseNanos
(String
String to parse.
s
,
int
Begin position at the string to read.
start
,
int
End position at the string to read.
end
)

Parse nanoseconds.

public static Value

Returns:

parsed time
parseTime
(String
string to parse
s
,
CastDataProvider
the cast information provider, or null
provider
,
boolean
if true return ValueTimeTimeZone instead of ValueTime
withTimeZone
)

Parses time value from the specified string.

public static long

Returns:

the time in nanoseconds
parseTimeNanos
(String
the string to parse
s
,
int
the parse index start
start
,
int
the parse index end
end
)

Parse a time string.

public static Value

Returns:

parsed timestamp
parseTimestamp
(String
string to parse
s
,
CastDataProvider
the cast information provider, may be null for Standard-compliant literals
provider
,
boolean
if true return ValueTimestampTimeZone instead of ValueTimestamp
withTimeZone
)

Parses timestamp value from the specified string.

public static void
resetCalendar()

Reset the cached calendar for default timezone, for example after changing the default timezone.

public static ValueTimestampTimeZone

Returns:

timestamp with time zone with new offset
timestampTimeZoneAtOffset
(long
the date value
dateValue
,
long
the nanoseconds since midnight
timeNanos
,
int
old offset
oldOffset
,
int
new offset
newOffset
)

Moves timestamp with time zone to a new time zone.

public static String

Returns:

time zone name
timeZoneNameFromOffsetSeconds
(int
time zone offset in seconds
offsetSeconds
)

Generates time zone name for the specified offset in seconds.

public static int

Returns:

the year
yearFromDateValue
(long
the date value
x
)

Get the year from a date value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EPOCH_DATE_VALUEback to summary
public static final int EPOCH_DATE_VALUE

Date value for 1970-01-01.

FRACTIONAL_SECONDS_TABLEback to summary
pack-priv static final int[] FRACTIONAL_SECONDS_TABLE

Multipliers for convertScale(long, int, long) and appendNanos(StringBuilder, int).

LOCALback to summary
private static volatile TimeZoneProvider LOCAL
MAX_DATE_VALUEback to summary
public static final long MAX_DATE_VALUE

Maximum possible date value.

MILLIS_PER_DAYback to summary
public static final long MILLIS_PER_DAY

The number of milliseconds per day.

MIN_DATE_VALUEback to summary
public static final long MIN_DATE_VALUE

Minimum possible date value.

NANOS_PER_DAYback to summary
public static final long NANOS_PER_DAY

The number of nanoseconds per day.

NANOS_PER_HOURback to summary
public static final long NANOS_PER_HOUR

The number of nanoseconds per hour.

NANOS_PER_MINUTEback to summary
public static final long NANOS_PER_MINUTE

The number of nanoseconds per minute.

NANOS_PER_SECONDback to summary
public static final long NANOS_PER_SECOND

The number of nanoseconds per second.

NORMAL_DAYS_PER_MONTHback to summary
private static final int[] NORMAL_DAYS_PER_MONTH
SECONDS_PER_DAYback to summary
public static final long SECONDS_PER_DAY

The number of seconds per day.

SHIFT_MONTHback to summary
public static final int SHIFT_MONTH

The offset of month bits in date values.

SHIFT_YEARback to summary
public static final int SHIFT_YEAR

The offset of year bits in date values.

Constructor Detail

DateTimeUtilsback to summary
private DateTimeUtils()

Method Detail

absoluteDayback to summary
pack-priv static long absoluteDay(long y, int m, int d)

Calculate the absolute day.

Parameters
y:long

year

m:int

month

d:int

day

Returns:long

the absolute day

absoluteDayFromDateValueback to summary
public static long absoluteDayFromDateValue(long dateValue)

Calculate the absolute day from an encoded date value.

Parameters
dateValue:long

the date value

Returns:long

the absolute day

absoluteDayFromYearback to summary
public static long absoluteDayFromYear(long year)

Calculate the absolute day for a January, 1 of the specified year.

Parameters
year:long

the year

Returns:long

the absolute day

appendDateback to summary
public static StringBuilder appendDate(StringBuilder builder, long dateValue)

Append a date to the string builder.

Parameters
builder:StringBuilder

the target string builder

dateValue:long

the date value

Returns:StringBuilder

the specified string builder

appendNanosback to summary
pack-priv static StringBuilder appendNanos(StringBuilder builder, int nanos)

Append nanoseconds of time, if any.

Parameters
builder:StringBuilder

string builder to append to

nanos:int

nanoseconds of second

Returns:StringBuilder

the specified string builder

appendTimeback to summary
public static StringBuilder appendTime(StringBuilder builder, long nanos)

Append a time to the string builder.

Parameters
builder:StringBuilder

the target string builder

nanos:long

the time in nanoseconds

Returns:StringBuilder

the specified string builder

appendTimeZoneback to summary
public static StringBuilder appendTimeZone(StringBuilder builder, int tz)

Append a time zone to the string builder.

Parameters
builder:StringBuilder

the target string builder

tz:int

the time zone offset in seconds

Returns:StringBuilder

the specified string builder

convertScaleback to summary
public static long convertScale(long nanosOfDay, int scale, long range)

Converts scale of nanoseconds.

Parameters
nanosOfDay:long

nanoseconds of day

scale:int

fractional seconds precision

range:long

the allowed range of values (0..range-1)

Returns:long

scaled value

currentTimestampback to summary
public static ValueTimestampTimeZone currentTimestamp(TimeZoneProvider timeZone)

Returns current timestamp.

Parameters
timeZone:TimeZoneProvider

the time zone

Returns:ValueTimestampTimeZone

current timestamp

currentTimestampback to summary
public static ValueTimestampTimeZone currentTimestamp(TimeZoneProvider timeZone, Instant now)

Returns current timestamp using the specified instant for its value.

Parameters
timeZone:TimeZoneProvider

the time zone

now:Instant

timestamp source, must be greater than or equal to 1970-01-01T00:00:00Z

Returns:ValueTimestampTimeZone

current timestamp

dateAndTimeFromValueback to summary
public static long[] dateAndTimeFromValue(Value value, CastDataProvider provider)

Extracts date value and nanos of day from the specified value.

Parameters
value:Value

value to extract fields from

provider:CastDataProvider

the cast information provider

Returns:long[]

array with date value and nanos of day

dateTimeToValueback to summary
public static Value dateTimeToValue(Value original, long dateValue, long timeNanos)

Creates a new date-time value with the same type as original value. If original value is a ValueTimestampTimeZone or ValueTimeTimeZone, returned value will have the same time zone offset as original value.

Parameters
original:Value

original value

dateValue:long

date value for the returned value

timeNanos:long

nanos of day for the returned value

Returns:Value

new value with specified date value and nanos of day

dateValueback to summary
public static long dateValue(long year, int month, int day)

Get the date value from a given date.

Parameters
year:long

the year

month:int

the month (1..12)

day:int

the day (1..31)

Returns:long

the date value

dateValueFromAbsoluteDayback to summary
public static long dateValueFromAbsoluteDay(long absoluteDay)

Calculate the encoded date value from an absolute day.

Parameters
absoluteDay:long

the absolute day

Returns:long

the date value

dateValueFromDenormalizedDateback to summary
public 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:long

the year

month:long

the month, if out of range month and year will be normalized

day:int

the day of the month, if out of range it will be saturated

Returns:long

the date value

dateValueFromLocalSecondsback to summary
public static long dateValueFromLocalSeconds(long localSeconds)

Convert a local seconds to an encoded date.

Parameters
localSeconds:long

the seconds since 1970-01-01

Returns:long

the date value

dayFromDateValueback to summary
public static int dayFromDateValue(long x)

Get the day of month from a date value.

Parameters
x:long

the date value

Returns:int

the day (1..31)

decrementDateValueback to summary
public static long decrementDateValue(long dateValue)

Return the previous date value.

Parameters
dateValue:long

the date value

Returns:long

the previous date value

getDayOfWeekback to summary
public static int getDayOfWeek(long dateValue, int firstDayOfWeek)

Returns day of week.

Parameters
dateValue:long

the date value

firstDayOfWeek:int

first day of week, Monday as 1, Sunday as 7 or 0

Returns:int

day of week

See Also
getIsoDayOfWeek(long)
getDayOfWeekFromAbsoluteback to summary
public static int getDayOfWeekFromAbsolute(long absoluteValue, int firstDayOfWeek)

Get the day of the week from the absolute day value.

Parameters
absoluteValue:long

the absolute day

firstDayOfWeek:int

the first day of the week

Returns:int

the day of week

getDayOfYearback to summary
public static int getDayOfYear(long dateValue)

Returns number of day in year.

Parameters
dateValue:long

the date value

Returns:int

number of day in year

getDaysInMonthback to summary
public static int getDaysInMonth(int year, int month)

Returns number of days in month.

Parameters
year:int

the year

month:int

the month

Returns:int

number of days in the specified month

getEpochSecondsback to summary
public static long getEpochSeconds(long dateValue, long timeNanos, int offsetSeconds)

Calculates the seconds since epoch for the specified date value, nanoseconds since midnight, and time zone offset.

Parameters
dateValue:long

date value

timeNanos:long

nanoseconds since midnight

offsetSeconds:int

time zone offset in seconds

Returns:long

seconds since epoch in UTC

getIsoDayOfWeekback to summary
public static int getIsoDayOfWeek(long dateValue)

Returns ISO day of week.

Parameters
dateValue:long

the date value

Returns:int

ISO day of week, Monday as 1 to Sunday as 7

See Also
getSundayDayOfWeek(long)
getIsoWeekOfYearback to summary
public static int getIsoWeekOfYear(long dateValue)

Returns ISO number of week in year.

Parameters
dateValue:long

the date value

Returns:int

number of week in year

See Also
getIsoWeekYear(long), getWeekOfYear(long, int, int)
getIsoWeekYearback to summary
public static int getIsoWeekYear(long dateValue)

Returns ISO week year.

Parameters
dateValue:long

the date value

Returns:int

ISO week year

See Also
getIsoWeekOfYear(long), getWeekYear(long, int, int)
getSundayDayOfWeekback to summary
public static int getSundayDayOfWeek(long dateValue)

Returns day of week with Sunday as 1.

Parameters
dateValue:long

the date value

Returns:int

day of week, Sunday as 1 to Monday as 7

See Also
getIsoDayOfWeek(long)
getTimeZoneback to summary
public static TimeZoneProvider getTimeZone()

Get the time zone provider for the default time zone.

Returns:TimeZoneProvider

the time zone provider for the default time zone

getWeekOfYearback to summary
public static int getWeekOfYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek)

Returns number of week in year.

Parameters
dateValue:long

the date value

firstDayOfWeek:int

first day of week, Monday as 1, Sunday as 7 or 0

minimalDaysInFirstWeek:int

minimal days in first week of year

Returns:int

number of week in year

See Also
getIsoWeekOfYear(long)
getWeekYearback to summary
public static int getWeekYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek)

Returns week year.

Parameters
dateValue:long

the date value

firstDayOfWeek:int

first day of week, Monday as 1, Sunday as 7 or 0

minimalDaysInFirstWeek:int

minimal days in first week of year

Returns:int

week year

See Also
getIsoWeekYear(long)
getWeekYearAbsoluteStartback to summary
public static long getWeekYearAbsoluteStart(int weekYear, int firstDayOfWeek, int minimalDaysInFirstWeek)

Get absolute day of the first day in the week year.

Parameters
weekYear:int

the week year

firstDayOfWeek:int

first day of week, Monday as 1, Sunday as 7 or 0

minimalDaysInFirstWeek:int

minimal days in first week of year

Returns:long

absolute day of the first day in the week year

incrementDateValueback to summary
public static long incrementDateValue(long dateValue)

Return the next date value.

Parameters
dateValue:long

the date value

Returns:long

the next date value

isLeapYearback to summary
pack-priv static boolean isLeapYear(int year)
isValidDateback to summary
public static boolean isValidDate(int year, int month, int day)

Verify if the specified date is valid.

Parameters
year:int

the year

month:int

the month (January is 1)

day:int

the day (1 is the first of the month)

Returns:boolean

true if it is valid

monthFromDateValueback to summary
public static int monthFromDateValue(long x)

Get the month from a date value.

Parameters
x:long

the date value

Returns:int

the month (1..12)

nanosFromLocalSecondsback to summary
public static long nanosFromLocalSeconds(long localSeconds)

Convert a time in seconds in local time to the nanoseconds since midnight.

Parameters
localSeconds:long

the seconds since 1970-01-01

Returns:long

the nanoseconds

normalizeNanosOfDayback to summary
public static long normalizeNanosOfDay(long nanos)

Calculate the normalized nanos of day.

Parameters
nanos:long

the nanoseconds (may be negative or larger than one day)

Returns:long

the nanos of day within a day

parseDateValueback to summary
public static long parseDateValue(String s, int start, int end)

Parse a date string. The format is: [+|-]year-month-day or [+|-]yyyyMMdd.

Parameters
s:String

the string to parse

start:int

the parse index start

end:int

the parse index end

Returns:long

the date value

Exceptions
IllegalArgumentException:
if there is a problem
parseNanosback to summary
pack-priv static int parseNanos(String s, int start, int end)

Parse nanoseconds.

Parameters
s:String

String to parse.

start:int

Begin position at the string to read.

end:int

End position at the string to read.

Returns:int

Parsed nanoseconds.

parseTimeback to summary
public static Value parseTime(String s, CastDataProvider provider, boolean withTimeZone)

Parses time value from the specified string.

Parameters
s:String

string to parse

provider:CastDataProvider

the cast information provider, or null

withTimeZone:boolean

if true return ValueTimeTimeZone instead of ValueTime

Returns:Value

parsed time

parseTimeNanosback to summary
public static long parseTimeNanos(String s, int start, int end)

Parse a time string. The format is: hour:minute[:second[.nanos]], hhmm[ss[.nanos]], or hour.minute.second[.nanos].

Parameters
s:String

the string to parse

start:int

the parse index start

end:int

the parse index end

Returns:long

the time in nanoseconds

Exceptions
IllegalArgumentException:
if there is a problem
parseTimestampback to summary
public static Value parseTimestamp(String s, CastDataProvider provider, boolean withTimeZone)

Parses timestamp value from the specified string.

Parameters
s:String

string to parse

provider:CastDataProvider

the cast information provider, may be null for Standard-compliant literals

withTimeZone:boolean

if true return ValueTimestampTimeZone instead of ValueTimestamp

Returns:Value

parsed timestamp

resetCalendarback to summary
public static void resetCalendar()

Reset the cached calendar for default timezone, for example after changing the default timezone.

timestampTimeZoneAtOffsetback to summary
public static ValueTimestampTimeZone timestampTimeZoneAtOffset(long dateValue, long timeNanos, int oldOffset, int newOffset)

Moves timestamp with time zone to a new time zone.

Parameters
dateValue:long

the date value

timeNanos:long

the nanoseconds since midnight

oldOffset:int

old offset

newOffset:int

new offset

Returns:ValueTimestampTimeZone

timestamp with time zone with new offset

timeZoneNameFromOffsetSecondsback to summary
public static String timeZoneNameFromOffsetSeconds(int offsetSeconds)

Generates time zone name for the specified offset in seconds.

Parameters
offsetSeconds:int

time zone offset in seconds

Returns:String

time zone name

yearFromDateValueback to summary
public static int yearFromDateValue(long x)

Get the year from a date value.

Parameters
x:long

the date value

Returns:int

the year