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 long |
MILLIS_PER_DAY
The number of milliseconds per day.
|
| Modifier and Type | Method and Description |
|---|---|
static long |
absoluteDayFromDateValue(long dateValue)
Calculate the absolute day from a date value.
|
static java.sql.Timestamp |
addMonths(java.sql.Timestamp refDate,
int nrOfMonthsToAdd)
Adds the number of months to the date.
|
static ValueDate |
convertDate(java.sql.Date x,
java.util.Calendar calendar)
Convert the 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 java.sql.Date |
convertDateValueToDate(long dateValue)
Convert a date value to a date, using the default timezone.
|
static java.sql.Timestamp |
convertDateValueToTimestamp(long dateValue,
long timeNanos)
Convert a date value / time value to a timestamp, using the default
timezone.
|
static java.sql.Time |
convertNanoToTime(long nanos)
Convert a time value to a time, using the default
timezone.
|
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 long |
convertToLocal(java.util.Date x,
java.util.Calendar target)
Convert a date to the specified time zone.
|
static long |
dateValue(long year,
int month,
int day)
Get the date value from a given date.
|
static long |
dateValueFromAbsoluteDay(long absoluteDay)
Calculate the date value from an absolute day.
|
static long |
dateValueFromDate(long ms)
Calculate the date value (in the default timezone) from a given time in
milliseconds in UTC.
|
static int |
dayFromDateValue(long x)
Get the day of month from a date value.
|
static java.lang.String |
formatDateTime(java.util.Date date,
java.lang.String format,
java.lang.String locale,
java.lang.String timeZone)
Formats a date using a format string.
|
static int |
getDatePart(java.util.Date d,
int field)
Get the specified field of a date, however with years normalized to
positive or negative, and month starting with 1.
|
static int |
getIsoDayOfWeek(java.util.Date date)
Return the day of week according to the ISO 8601 specification.
|
static int |
getIsoWeek(java.util.Date date)
Returns the week of the year according to the ISO 8601 specification.
|
static int |
getIsoYear(java.util.Date date)
Returns the year according to the ISO week definition.
|
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 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 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)
Calculate the nanoseconds since midnight (in the default timezone) from a
given time in milliseconds in UTC.
|
static ValueTimestamp |
normalizeTimestamp(long absoluteDay,
long nanos)
Calculate the normalized timestamp.
|
static java.util.Date |
parseDateTime(java.lang.String date,
java.lang.String format,
java.lang.String locale,
java.lang.String timeZone)
Parses a date using a format string.
|
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 void |
resetCalendar()
Reset the cached calendar for default timezone, for example after
changing the default timezone.
|
static int |
yearFromDateValue(long x)
Get the year from a date value.
|
public static final long MILLIS_PER_DAY
public static void resetCalendar()
public 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 long convertToLocal(java.util.Date x,
java.util.Calendar target)
x - the date to converttarget - the calendar with the target timezonepublic 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 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 int getDatePart(java.util.Date d,
int field)
d - the datefield - the field typepublic 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 getIsoDayOfWeek(java.util.Date date)
date - the date object which day of week should be calculatedpublic static int getIsoWeek(java.util.Date date)
date - the date object which week of year should be calculatedpublic static int getIsoYear(java.util.Date date)
date - the date object which year should be calculatedpublic static java.lang.String formatDateTime(java.util.Date date,
java.lang.String format,
java.lang.String locale,
java.lang.String timeZone)
date - the date to formatformat - the format stringlocale - the localetimeZone - the timezonepublic static java.util.Date parseDateTime(java.lang.String date,
java.lang.String format,
java.lang.String locale,
java.lang.String timeZone)
date - the date to parseformat - the parsing formatlocale - the localetimeZone - the timeZonepublic 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 java.sql.Timestamp convertDateValueToTimestamp(long dateValue,
long timeNanos)
dateValue - the date valuetimeNanos - the nanoseconds since midnightpublic static java.sql.Time convertNanoToTime(long nanos)
nanos - 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 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 long absoluteDayFromDateValue(long dateValue)
dateValue - the date valuepublic static long dateValueFromAbsoluteDay(long absoluteDay)
absoluteDay - the absolute daypublic static java.sql.Timestamp addMonths(java.sql.Timestamp refDate,
int nrOfMonthsToAdd)
refDate - the original datenrOfMonthsToAdd - the number of months to add