public class DateTimeUtils
extends java.lang.Object
Date value: a bit field with bits for the year, month, and day. Absolute day: the day number (0 means 1970-01-01).
| Modifier and Type | Field and Description |
|---|---|
static int |
EPOCH_DATE_VALUE
Date value for 1970-01-01.
|
static long |
MILLIS_PER_DAY
The number of milliseconds per day.
|
static long |
NANOS_PER_DAY
The number of nanoseconds per day.
|
static long |
SECONDS_PER_DAY
The number of seconds per day.
|
static java.util.TimeZone |
UTC
UTC time zone.
|
| Modifier and Type | Method and Description |
|---|---|
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 void |
appendDate(java.lang.StringBuilder buff,
long dateValue)
Append a date to the string builder.
|
static void |
appendTime(java.lang.StringBuilder buff,
long nanos)
Append a time to the string builder.
|
static void |
appendTimeZone(java.lang.StringBuilder buff,
short tz)
Append a time zone to the string builder.
|
static ValueDate |
convertDate(java.sql.Date x,
java.util.Calendar calendar)
Convert a java.util.Date using the specified calendar.
|
static java.sql.Date |
convertDate(Value value,
java.util.Calendar calendar)
Convert the date to the specified time zone.
|
static long |
convertDateTimeValueToMillis(java.util.TimeZone tz,
long dateValue,
long ms)
Convert an encoded date-time value to millis, using the supplied timezone.
|
static java.sql.Date |
convertDateValueToDate(long dateValue)
Convert an encoded date value to a java.util.Date, using the default
timezone.
|
static java.sql.Timestamp |
convertDateValueToTimestamp(long dateValue,
long timeNanos)
Convert an encoded date value / time value to a timestamp, using the
default timezone.
|
static java.sql.Time |
convertNanoToTime(long nanosSinceMidnight)
Convert a time value to a time, using the default timezone.
|
static long |
convertScale(long nanosOfDay,
int scale)
Converts scale of nanoseconds.
|
static ValueTime |
convertTime(java.sql.Time x,
java.util.Calendar calendar)
Convert the time using the specified calendar.
|
static java.sql.Time |
convertTime(Value value,
java.util.Calendar calendar)
Convert the time to the specified time zone.
|
static ValueTimestamp |
convertTimestamp(java.sql.Timestamp x,
java.util.Calendar calendar)
Convert the timestamp using the specified calendar.
|
static java.sql.Timestamp |
convertTimestamp(Value value,
java.util.Calendar calendar)
Convert the timestamp to the specified time zone.
|
static java.sql.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 java.util.GregorianCalendar |
createGregorianCalendar()
Creates a Gregorian calendar for the default timezone using the default
locale.
|
static java.util.GregorianCalendar |
createGregorianCalendar(java.util.TimeZone tz)
Creates a Gregorian calendar for the given timezone using the default
locale.
|
static long[] |
dateAndTimeFromValue(Value value)
Extracts date value and nanos of day from the specified 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.
|
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 |
dateValueFromDate(long ms)
Convert a UTC datetime in millis to an encoded date in the default
timezone.
|
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 int |
dayFromDateValue(long x)
Get the day of month from a date value.
|
static long |
decrementDateValue(long dateValue)
Return the previous date value.
|
static java.util.GregorianCalendar |
getCalendar()
Get a calendar for the default timezone.
|
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 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 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(java.util.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 int |
getSundayDayOfWeek(long dateValue)
Returns day of week with Sunday as 1.
|
static long |
getTimeLocalWithoutDst(java.util.Date d)
Get the number of milliseconds since 1970-01-01 in the local timezone,
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.
|
static int |
getTimeZoneOffsetMillis(java.util.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 |
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 |
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 |
nanosFromDate(long ms)
Convert a time in milliseconds in UTC to the nanoseconds since midnight
(in the default timezone).
|
static ValueTimestamp |
normalizeTimestamp(long absoluteDay,
long nanos)
Calculate the normalized timestamp.
|
static long |
parseDateValue(java.lang.String s,
int start,
int end)
Parse a date string.
|
static long |
parseTimeNanos(java.lang.String s,
int start,
int end,
boolean timeOfDay)
Parse a time string.
|
static Value |
parseTimestamp(java.lang.String s,
Mode mode,
boolean withTimeZone)
Parses timestamp value from the specified string.
|
static long |
prolepticGregorianAbsoluteDayFromDateValue(long dateValue)
Calculate the absolute day from an encoded date value in proleptic Gregorian
calendar.
|
static void |
resetCalendar()
Reset the cached calendar for default timezone, for example after
changing the default timezone.
|
static ValueTimestampTimeZone |
timestampTimeZoneFromLocalDateValueAndNanos(long dateValue,
long timeNanos)
Converts local date value and nanoseconds to timestamp with time zone.
|
static java.lang.String |
timestampTimeZoneToString(long dateValue,
long timeNanos,
short timeZoneOffsetMins)
Formats timestamp with time zone as string.
|
static java.lang.String |
timeZoneNameFromOffsetMins(int offsetMins)
Generates time zone name for the specified offset in minutes.
|
static int |
yearFromDateValue(long x)
Get the year from a date value.
|
public static final long MILLIS_PER_DAY
public static final long SECONDS_PER_DAY
public static final java.util.TimeZone UTC
public static final long NANOS_PER_DAY
public static final int EPOCH_DATE_VALUE
public static void resetCalendar()
public static java.util.GregorianCalendar getCalendar()
public static java.util.GregorianCalendar createGregorianCalendar()
public static java.util.GregorianCalendar createGregorianCalendar(java.util.TimeZone tz)
tz - timezone for the calendar, is never nullpublic static java.sql.Date convertDate(Value value, java.util.Calendar calendar)
value - the date (might be ValueNull)calendar - the calendarpublic static java.sql.Time convertTime(Value value, java.util.Calendar calendar)
value - the time (might be ValueNull)calendar - the calendarpublic static java.sql.Timestamp convertTimestamp(Value value, java.util.Calendar calendar)
value - the timestamp (might be ValueNull)calendar - the calendarpublic static ValueDate convertDate(java.sql.Date x, java.util.Calendar calendar)
x - the datecalendar - the calendarpublic static ValueTime convertTime(java.sql.Time x, java.util.Calendar calendar)
x - the timecalendar - the calendarpublic static ValueTimestamp convertTimestamp(java.sql.Timestamp x, java.util.Calendar calendar)
x - the timecalendar - the calendarpublic static long parseDateValue(java.lang.String s,
int start,
int end)
s - the string to parsestart - the parse index startend - the parse index endjava.lang.IllegalArgumentException - if there is a problempublic static long parseTimeNanos(java.lang.String s,
int start,
int end,
boolean timeOfDay)
s - the string to parsestart - the parse index startend - the parse index endtimeOfDay - whether the result need to be within 0 (inclusive) and 1
day (exclusive)java.lang.IllegalArgumentException - if there is a problempublic static Value parseTimestamp(java.lang.String s, Mode mode, boolean withTimeZone)
s - string to parsemode - database mode, or nullwithTimeZone - if true return ValueTimestampTimeZone instead of
ValueTimestamppublic static int getTimeZoneOffsetMillis(java.util.TimeZone tz,
long dateValue,
long timeNanos)
tz - time zone, or null for defaultdateValue - date valuetimeNanos - nanoseconds since midnightpublic static long getMillis(long dateValue,
long timeNanos,
short offsetMins)
dateValue - date valuetimeNanos - nanoseconds since midnightoffsetMins - time zone offset in minutespublic static long getMillis(java.util.TimeZone tz,
int year,
int month,
int day,
int hour,
int minute,
int second,
int millis)
tz - the timezone of the parameters, or null for the default
timezoneyear - 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 millisecondspublic static long[] dateAndTimeFromValue(Value value)
value - value to extract fields frompublic static Value dateTimeToValue(Value original, long dateValue, long timeNanos, boolean forceTimestamp)
original - original valuedateValue - date value for the returned valuetimeNanos - nanos of day for the returned valueforceTimestamp - if true return ValueTimestamp if original argument is
ValueDate or ValueTimepublic static long getTimeLocalWithoutDst(java.util.Date d)
d - the datepublic static long getTimeUTCWithoutDst(long millis)
millis - the number of milliseconds in the local timezonepublic static int getDayOfWeek(long dateValue,
int firstDayOfWeek)
dateValue - the date valuefirstDayOfWeek - first day of week, Monday as 1, Sunday as 7 or 0getIsoDayOfWeek(long)public static int getDayOfWeekFromAbsolute(long absoluteValue,
int firstDayOfWeek)
absoluteValue - the absolute dayfirstDayOfWeek - the first day of the weekpublic static int getDayOfYear(long dateValue)
dateValue - the date valuepublic static int getIsoDayOfWeek(long dateValue)
dateValue - the date valuegetSundayDayOfWeek(long)public static int getIsoWeekOfYear(long dateValue)
dateValue - the date valuegetIsoWeekYear(long),
getWeekOfYear(long, int, int)public static int getIsoWeekYear(long dateValue)
dateValue - the date valuegetIsoWeekOfYear(long),
getWeekYear(long, int, int)public static int getSundayDayOfWeek(long dateValue)
dateValue - the date valuegetIsoDayOfWeek(long)public static int getWeekOfYear(long dateValue,
int firstDayOfWeek,
int minimalDaysInFirstWeek)
dateValue - the date valuefirstDayOfWeek - first day of week, Monday as 1, Sunday as 7 or 0minimalDaysInFirstWeek - minimal days in first week of yeargetIsoWeekOfYear(long)public static int getWeekYear(long dateValue,
int firstDayOfWeek,
int minimalDaysInFirstWeek)
dateValue - the date valuefirstDayOfWeek - first day of week, Monday as 1, Sunday as 7 or 0minimalDaysInFirstWeek - minimal days in first week of yeargetIsoWeekYear(long)public static int getDaysInMonth(int year,
int month)
year - the yearmonth - the monthpublic static boolean isValidDate(int year,
int month,
int day)
year - the yearmonth - the month (January is 1)day - the day (1 is the first of the month)public static java.sql.Date convertDateValueToDate(long dateValue)
dateValue - the date valuepublic static long convertDateTimeValueToMillis(java.util.TimeZone tz,
long dateValue,
long ms)
tz - the timezonedateValue - the date valuems - milliseconds of daypublic static java.sql.Timestamp convertDateValueToTimestamp(long dateValue,
long timeNanos)
dateValue - the date valuetimeNanos - the nanoseconds since midnightpublic static java.sql.Timestamp convertTimestampTimeZoneToTimestamp(long dateValue,
long timeNanos,
short offsetMins)
dateValue - the date valuetimeNanos - the nanoseconds since midnightoffsetMins - time zone offset in minutespublic static java.sql.Time convertNanoToTime(long nanosSinceMidnight)
nanosSinceMidnight - the nanoseconds since midnightpublic static int yearFromDateValue(long x)
x - the date valuepublic static int monthFromDateValue(long x)
x - the date valuepublic static int dayFromDateValue(long x)
x - the date valuepublic static long dateValue(long year,
int month,
int day)
year - the yearmonth - the month (1..12)day - the day (1..31)public static long dateValueFromDenormalizedDate(long year,
long month,
int day)
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 saturatedpublic static long dateValueFromDate(long ms)
ms - the millisecondspublic static long nanosFromDate(long ms)
ms - the millisecondspublic static ValueTimestamp normalizeTimestamp(long absoluteDay, long nanos)
absoluteDay - the absolute daynanos - the nanoseconds (may be negative or larger than one day)public static ValueTimestampTimeZone timestampTimeZoneFromLocalDateValueAndNanos(long dateValue, long timeNanos)
dateValue - date valuetimeNanos - nanoseconds since midnightpublic static long absoluteDayFromYear(long year)
year - the yearpublic static long absoluteDayFromDateValue(long dateValue)
dateValue - the date valuepublic static long prolepticGregorianAbsoluteDayFromDateValue(long dateValue)
dateValue - the date valuepublic static long dateValueFromAbsoluteDay(long absoluteDay)
absoluteDay - the absolute daypublic static long incrementDateValue(long dateValue)
dateValue - the date valuepublic static long decrementDateValue(long dateValue)
dateValue - the date valuepublic static void appendDate(java.lang.StringBuilder buff,
long dateValue)
buff - the target string builderdateValue - the date valuepublic static void appendTime(java.lang.StringBuilder buff,
long nanos)
buff - the target string buildernanos - the time in nanosecondspublic static void appendTimeZone(java.lang.StringBuilder buff,
short tz)
buff - the target string buildertz - the time zone in minutespublic static java.lang.String timestampTimeZoneToString(long dateValue,
long timeNanos,
short timeZoneOffsetMins)
dateValue - the year-month-day bit fieldtimeNanos - nanoseconds since midnighttimeZoneOffsetMins - the time zone offset in minutespublic static java.lang.String timeZoneNameFromOffsetMins(int offsetMins)
offsetMins - offset in minutespublic static long convertScale(long nanosOfDay,
int scale)
nanosOfDay - nanoseconds of dayscale - fractional seconds precision