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 intDate value for 1970-01-01.static final longMaximum possible date value.static final longThe number of milliseconds per day.static final longMinimum possible date value.static final longThe number of nanoseconds per day.static final longThe number of nanoseconds per hour.static final longThe number of nanoseconds per minute.static final longThe number of nanoseconds per second.static final longThe number of seconds per day.static final intThe offset of month bits in date values.static final intThe offset of year bits in date values. -
Method Summary
Modifier and TypeMethodDescriptionstatic longabsoluteDayFromDateValue(long dateValue) Calculate the absolute day from an encoded date value.static longabsoluteDayFromYear(long year) Calculate the absolute day for a January, 1 of the specified year.static StringBuilderappendDate(StringBuilder builder, long dateValue) Append a date to the string builder.static StringBuilderappendTime(StringBuilder builder, long nanos) Append a time to the string builder.static StringBuilderappendTimeZone(StringBuilder builder, int tz) Append a time zone to the string builder.static longconvertScale(long nanosOfDay, int scale, long range) Converts scale of nanoseconds.static ValueTimestampTimeZonecurrentTimestamp(TimeZoneProvider timeZone) Returns current timestamp.static ValueTimestampTimeZonecurrentTimestamp(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 ValuedateTimeToValue(Value original, long dateValue, long timeNanos) Creates a new date-time value with the same type as original value.static longdateValue(long year, int month, int day) Get the date value from a given date.static longdateValueFromAbsoluteDay(long absoluteDay) Calculate the encoded date value from an absolute day.static longdateValueFromDenormalizedDate(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 longdateValueFromLocalSeconds(long localSeconds) Convert a local seconds to an encoded date.static intdayFromDateValue(long x) Get the day of month from a date value.static longdecrementDateValue(long dateValue) Return the previous date value.static intgetDayOfWeek(long dateValue, int firstDayOfWeek) Returns day of week.static intgetDayOfWeekFromAbsolute(long absoluteValue, int firstDayOfWeek) Get the day of the week from the absolute day value.static intgetDayOfYear(long dateValue) Returns number of day in year.static intgetDaysInMonth(int year, int month) Returns number of days in month.static longgetEpochSeconds(long dateValue, long timeNanos, int offsetSeconds) Calculates the seconds since epoch for the specified date value, nanoseconds since midnight, and time zone offset.static intgetIsoDayOfWeek(long dateValue) Returns ISO day of week.static intgetIsoWeekOfYear(long dateValue) Returns ISO number of week in year.static intgetIsoWeekYear(long dateValue) Returns ISO week year.static intgetSundayDayOfWeek(long dateValue) Returns day of week with Sunday as 1.static TimeZoneProviderGet the time zone provider for the default time zone.static intgetWeekOfYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek) Returns number of week in year.static intgetWeekYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek) Returns week year.static longgetWeekYearAbsoluteStart(int weekYear, int firstDayOfWeek, int minimalDaysInFirstWeek) Get absolute day of the first day in the week year.static longincrementDateValue(long dateValue) Return the next date value.static booleanisValidDate(int year, int month, int day) Verify if the specified date is valid.static intmonthFromDateValue(long x) Get the month from a date value.static longnanosFromLocalSeconds(long localSeconds) Convert a time in seconds in local time to the nanoseconds since midnight.static longnormalizeNanosOfDay(long nanos) Calculate the normalized nanos of day.static longparseDateValue(String s, int start, int end) Parse a date string.static ValueparseTime(String s, CastDataProvider provider, boolean withTimeZone) Parses time value from the specified string.static longparseTimeNanos(String s, int start, int end) Parse a time string.static ValueparseTimestamp(String s, CastDataProvider provider, boolean withTimeZone) Parses timestamp value from the specified string.static voidReset the cached calendar for default timezone, for example after changing the default timezone.static ValueTimestampTimeZonetimestampTimeZoneAtOffset(long dateValue, long timeNanos, int oldOffset, int newOffset) Moves timestamp with time zone to a new time zone.static StringtimeZoneNameFromOffsetSeconds(int offsetSeconds) Generates time zone name for the specified offset in seconds.static intyearFromDateValue(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:
-
-
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
-
parseTimestamp
Parses timestamp value from the specified string.- Parameters:
s- string to parseprovider- the cast information provider, may benullfor Standard-compliant literalswithTimeZone- iftruereturnValueTimestampTimeZoneinstead ofValueTimestamp- Returns:
- parsed timestamp
-
parseTime
Parses time value from the specified string.- Parameters:
s- string to parseprovider- the cast information provider, ornullwithTimeZone- iftruereturnValueTimeTimeZoneinstead ofValueTime- Returns:
- parsed time
-
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
-
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
-
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
-