Package org.h2.util
Class DateTimeUtils
java.lang.Object
org.h2.util.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).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Date value for 1970-01-01.private static final int[]
Multipliers forconvertScale(long, int, long)
andappendNanos(StringBuilder, int)
.private static TimeZoneProvider
static final long
Maximum possible date value.static final long
The number of milliseconds per day.static final long
Minimum possible date value.static final long
The number of nanoseconds per day.static final long
The number of nanoseconds per hour.static final long
The number of nanoseconds per minute.static final long
The number of nanoseconds per second.private static final int[]
static final long
The number of seconds per day.static final int
The offset of month bits in date values.static final int
The offset of year bits in date values. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static long
absoluteDay
(long y, int m, int d) Calculate the absolute day.static long
absoluteDayFromDateValue
(long dateValue) Calculate the absolute day from an encoded date value.static long
absoluteDayFromYear
(long year) Calculate the absolute day for a January, 1 of the specified year.static StringBuilder
appendDate
(StringBuilder builder, long dateValue) Append a date to the string builder.(package private) static StringBuilder
appendNanos
(StringBuilder builder, int nanos) Append nanoseconds of time, if any.static StringBuilder
appendTime
(StringBuilder builder, long nanos) Append a time to the string builder.static StringBuilder
appendTimeZone
(StringBuilder builder, int tz) Append a time zone to the string builder.static long
convertScale
(long nanosOfDay, int scale, long range) Converts scale of nanoseconds.static ValueTimestampTimeZone
currentTimestamp
(TimeZoneProvider timeZone) Returns current timestamp.static ValueTimestampTimeZone
currentTimestamp
(TimeZoneProvider timeZone, Instant now) Returns current timestamp using the specified instant for its value.static long[]
dateAndTimeFromValue
(Value value, CastDataProvider provider) Extracts date value and nanos of day from the specified value.static Value
dateTimeToValue
(Value original, long dateValue, long timeNanos) Creates a new date-time value with the same type as original value.static long
dateValue
(long year, int month, int day) Get the date value from a given date.static long
dateValueFromAbsoluteDay
(long absoluteDay) Calculate the encoded date value from an absolute 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.static long
dateValueFromLocalSeconds
(long localSeconds) Convert a local seconds to an encoded date.static int
dayFromDateValue
(long x) Get the day of month from a date value.static long
decrementDateValue
(long dateValue) Return the previous date value.static int
getDayOfWeek
(long dateValue, int firstDayOfWeek) Returns day of week.static int
getDayOfWeekFromAbsolute
(long absoluteValue, int firstDayOfWeek) Get the day of the week from the absolute day value.static int
getDayOfYear
(long dateValue) Returns number of day in year.static int
getDaysInMonth
(int year, int month) Returns number of days in month.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.static int
getIsoDayOfWeek
(long dateValue) Returns ISO day of week.static int
getIsoWeekOfYear
(long dateValue) Returns ISO number of week in year.static int
getIsoWeekYear
(long dateValue) Returns ISO week year.static int
getSundayDayOfWeek
(long dateValue) Returns day of week with Sunday as 1.static TimeZoneProvider
Get the time zone provider for the default time zone.static int
getWeekOfYear
(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek) Returns number of week in year.static int
getWeekYear
(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek) Returns week year.static long
getWeekYearAbsoluteStart
(int weekYear, int firstDayOfWeek, int minimalDaysInFirstWeek) Get absolute day of the first day in the week year.static long
incrementDateValue
(long dateValue) Return the next date value.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
nanosFromLocalSeconds
(long localSeconds) Convert a time in seconds in local time to the nanoseconds since midnight.static long
normalizeNanosOfDay
(long nanos) Calculate the normalized nanos of day.static long
parseDateValue
(String s, int start, int end) Parse a date string.(package private) static int
parseNanos
(String s, int start, int end) Parse nanoseconds.static long
parseTimeNanos
(String s, int start, int end) Parse a time string.static Value
parseTimestamp
(String s, CastDataProvider provider, boolean withTimeZone) Parses timestamp value from the specified string.static ValueTimeTimeZone
parseTimeWithTimeZone
(String s, CastDataProvider provider) Parses TIME WITH TIME ZONE value from the specified string.static void
Reset the cached calendar for default timezone, for example after changing the default timezone.static ValueTimestampTimeZone
timestampTimeZoneAtOffset
(long dateValue, long timeNanos, int oldOffset, int newOffset) Moves timestamp with time zone to a new time zone.static String
timeZoneNameFromOffsetSeconds
(int offsetSeconds) Generates time zone name for the specified offset in seconds.static int
yearFromDateValue
(long x) Get the year from a date value.
-
Field Details
-
MILLIS_PER_DAY
public static final long MILLIS_PER_DAYThe number of milliseconds per day.- See Also:
-
SECONDS_PER_DAY
public static final long SECONDS_PER_DAYThe number of seconds per day.- See Also:
-
NANOS_PER_SECOND
public static final long NANOS_PER_SECONDThe number of nanoseconds per second.- See Also:
-
NANOS_PER_MINUTE
public static final long NANOS_PER_MINUTEThe number of nanoseconds per minute.- See Also:
-
NANOS_PER_HOUR
public static final long NANOS_PER_HOURThe number of nanoseconds per hour.- See Also:
-
NANOS_PER_DAY
public static final long NANOS_PER_DAYThe number of nanoseconds per day.- See Also:
-
SHIFT_YEAR
public static final int SHIFT_YEARThe offset of year bits in date values.- See Also:
-
SHIFT_MONTH
public static final int SHIFT_MONTHThe offset of month bits in date values.- See Also:
-
EPOCH_DATE_VALUE
public static final int EPOCH_DATE_VALUEDate value for 1970-01-01.- See Also:
-
MIN_DATE_VALUE
public static final long MIN_DATE_VALUEMinimum possible date value.- See Also:
-
MAX_DATE_VALUE
public static final long MAX_DATE_VALUEMaximum possible date value.- See Also:
-
NORMAL_DAYS_PER_MONTH
private static final int[] NORMAL_DAYS_PER_MONTH -
FRACTIONAL_SECONDS_TABLE
private static final int[] FRACTIONAL_SECONDS_TABLEMultipliers forconvertScale(long, int, long)
andappendNanos(StringBuilder, int)
. -
LOCAL
-
-
Constructor Details
-
DateTimeUtils
private DateTimeUtils()
-
-
Method Details
-
resetCalendar
public static void resetCalendar()Reset the cached calendar for default timezone, for example after changing the default timezone. -
getTimeZone
Get the time zone provider for the default time zone.- Returns:
- the time zone provider for the default time zone
-
currentTimestamp
Returns current timestamp.- Parameters:
timeZone
- the time zone- Returns:
- current timestamp
-
currentTimestamp
Returns current timestamp using the specified instant for its value.- Parameters:
timeZone
- the time zonenow
- timestamp source, must be greater than or equal to 1970-01-01T00:00:00Z- Returns:
- current timestamp
-
parseDateValue
Parse a date string. The format is: [+|-]year-month-day or [+|-]yyyyMMdd.- Parameters:
s
- the string to parsestart
- the parse index startend
- the parse index end- Returns:
- the date value
- Throws:
IllegalArgumentException
- if there is a problem
-
parseTimeNanos
Parse a time string. The format is: hour:minute[:second[.nanos]], hhmm[ss[.nanos]], or hour.minute.second[.nanos].- Parameters:
s
- the string to parsestart
- the parse index startend
- the parse index end- Returns:
- the time in nanoseconds
- Throws:
IllegalArgumentException
- if there is a problem
-
parseNanos
Parse nanoseconds.- Parameters:
s
- String to parse.start
- Begin position at the string to read.end
- End position at the string to read.- Returns:
- Parsed nanoseconds.
-
parseTimestamp
Parses timestamp value from the specified string.- Parameters:
s
- string to parseprovider
- the cast information provider, may benull
for Standard-compliant literalswithTimeZone
- iftrue
returnValueTimestampTimeZone
instead ofValueTimestamp
- Returns:
- parsed timestamp
-
parseTimeWithTimeZone
Parses TIME WITH TIME ZONE value from the specified string.- Parameters:
s
- string to parseprovider
- the cast information provider, ornull
- Returns:
- parsed time with time zone
-
getEpochSeconds
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
- date valuetimeNanos
- nanoseconds since midnightoffsetSeconds
- time zone offset in seconds- Returns:
- seconds since epoch in UTC
-
dateAndTimeFromValue
Extracts date value and nanos of day from the specified value.- Parameters:
value
- value to extract fields fromprovider
- the cast information provider- Returns:
- array with date value and nanos of day
-
dateTimeToValue
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
- original valuedateValue
- date value for the returned valuetimeNanos
- nanos of day for the returned value- Returns:
- new value with specified date value and nanos of day
-
getDayOfWeek
public static int getDayOfWeek(long dateValue, int firstDayOfWeek) Returns day of week.- Parameters:
dateValue
- the date valuefirstDayOfWeek
- first day of week, Monday as 1, Sunday as 7 or 0- Returns:
- day of week
- See Also:
-
getDayOfWeekFromAbsolute
public static int getDayOfWeekFromAbsolute(long absoluteValue, int firstDayOfWeek) Get the day of the week from the absolute day value.- Parameters:
absoluteValue
- the absolute dayfirstDayOfWeek
- the first day of the week- Returns:
- the day of week
-
getDayOfYear
public static int getDayOfYear(long dateValue) Returns number of day in year.- Parameters:
dateValue
- the date value- Returns:
- number of day in year
-
getIsoDayOfWeek
public 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
- See Also:
-
getIsoWeekOfYear
public static int getIsoWeekOfYear(long dateValue) Returns ISO number of week in year.- Parameters:
dateValue
- the date value- Returns:
- number of week in year
- See Also:
-
getIsoWeekYear
public static int getIsoWeekYear(long dateValue) Returns ISO week year.- Parameters:
dateValue
- the date value- Returns:
- ISO week year
- See Also:
-
getSundayDayOfWeek
public 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
- See Also:
-
getWeekOfYear
public static int getWeekOfYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek) Returns number of week in year.- Parameters:
dateValue
- the date valuefirstDayOfWeek
- first day of week, Monday as 1, Sunday as 7 or 0minimalDaysInFirstWeek
- minimal days in first week of year- Returns:
- number of week in year
- See Also:
-
getWeekYearAbsoluteStart
public static long getWeekYearAbsoluteStart(int weekYear, int firstDayOfWeek, int minimalDaysInFirstWeek) Get absolute day of the first day in the week year.- Parameters:
weekYear
- the week yearfirstDayOfWeek
- first day of week, Monday as 1, Sunday as 7 or 0minimalDaysInFirstWeek
- minimal days in first week of year- Returns:
- absolute day of the first day in the week year
-
getWeekYear
public static int getWeekYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek) Returns week year.- Parameters:
dateValue
- the date valuefirstDayOfWeek
- first day of week, Monday as 1, Sunday as 7 or 0minimalDaysInFirstWeek
- minimal days in first week of year- Returns:
- week year
- See Also:
-
getDaysInMonth
public static int getDaysInMonth(int year, int month) Returns number of days in month.- Parameters:
year
- the yearmonth
- the month- Returns:
- number of days in the specified month
-
isValidDate
public static boolean isValidDate(int year, int month, int day) Verify if the specified date is valid.- Parameters:
year
- the yearmonth
- the month (January is 1)day
- the day (1 is the first of the month)- Returns:
- true if it is valid
-
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 yearmonth
- the month (1..12)day
- the day (1..31)- Returns:
- the date value
-
dateValueFromDenormalizedDate
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
- the yearmonth
- the month, if out of range month and year will be normalizedday
- the day of the month, if out of range it will be saturated- Returns:
- the date value
-
dateValueFromLocalSeconds
public static long dateValueFromLocalSeconds(long localSeconds) Convert a local seconds to an encoded date.- Parameters:
localSeconds
- the seconds since 1970-01-01- Returns:
- the date value
-
nanosFromLocalSeconds
public static long nanosFromLocalSeconds(long localSeconds) Convert a time in seconds in local time to the nanoseconds since midnight.- Parameters:
localSeconds
- the seconds since 1970-01-01- Returns:
- the nanoseconds
-
normalizeNanosOfDay
public static long normalizeNanosOfDay(long nanos) Calculate the normalized nanos of day.- Parameters:
nanos
- the nanoseconds (may be negative or larger than one day)- Returns:
- the nanos of day within a day
-
absoluteDayFromYear
public 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
-
absoluteDayFromDateValue
public static long absoluteDayFromDateValue(long dateValue) Calculate the absolute day from an encoded date value.- Parameters:
dateValue
- the date value- Returns:
- the absolute day
-
absoluteDay
static long absoluteDay(long y, int m, int d) Calculate the absolute day.- Parameters:
y
- yearm
- monthd
- day- Returns:
- the absolute day
-
dateValueFromAbsoluteDay
public static long dateValueFromAbsoluteDay(long absoluteDay) Calculate the encoded date value from an absolute day.- Parameters:
absoluteDay
- the absolute day- Returns:
- the date value
-
incrementDateValue
public static long incrementDateValue(long dateValue) Return the next date value.- Parameters:
dateValue
- the date value- Returns:
- the next date value
-
decrementDateValue
public static long decrementDateValue(long dateValue) Return the previous date value.- Parameters:
dateValue
- the date value- Returns:
- the previous date value
-
appendDate
Append a date to the string builder.- Parameters:
builder
- the target string builderdateValue
- the date value- Returns:
- the specified string builder
-
appendTime
Append a time to the string builder.- Parameters:
builder
- the target string buildernanos
- the time in nanoseconds- Returns:
- the specified string builder
-
appendNanos
Append nanoseconds of time, if any.- Parameters:
builder
- string builder to append tonanos
- nanoseconds of second- Returns:
- the specified string builder
-
appendTimeZone
Append a time zone to the string builder.- Parameters:
builder
- the target string buildertz
- the time zone offset in seconds- Returns:
- the specified string builder
-
timeZoneNameFromOffsetSeconds
Generates time zone name for the specified offset in seconds.- Parameters:
offsetSeconds
- time zone offset in seconds- Returns:
- time zone name
-
convertScale
public static long convertScale(long nanosOfDay, int scale, long range) Converts scale of nanoseconds.- Parameters:
nanosOfDay
- nanoseconds of dayscale
- fractional seconds precisionrange
- the allowed range of values (0..range-1)- Returns:
- scaled value
-
timestampTimeZoneAtOffset
public static ValueTimestampTimeZone timestampTimeZoneAtOffset(long dateValue, long timeNanos, int oldOffset, int newOffset) Moves timestamp with time zone to a new time zone.- Parameters:
dateValue
- the date valuetimeNanos
- the nanoseconds since midnightoldOffset
- old offsetnewOffset
- new offset- Returns:
- timestamp with time zone with new offset
-