public class DateTimeUtils extends Object
Used by the JDBC driver.
TODO: review methods for performance. Due to allocations required, it may be preferable to introduce a "formatter" with the required state.
Modifier and Type | Class and Description |
---|---|
static class |
DateTimeUtils.PrecisionTime
|
Modifier and Type | Field and Description |
---|---|
static String |
DATE_FORMAT_STRING
The SimpleDateFormat string for ISO dates, "yyyy-MM-dd".
|
static TimeZone |
DEFAULT_ZONE
The Java default time zone.
|
static int |
EPOCH_JULIAN
The julian date of the epoch, 1970-01-01.
|
static TimeZone |
GMT_ZONE
Deprecated.
Use
UTC_ZONE |
static long |
MILLIS_PER_DAY
The number of milliseconds in a day.
|
static long |
MILLIS_PER_HOUR
The number of milliseconds in an hour.
|
static long |
MILLIS_PER_MINUTE
The number of milliseconds in a minute.
|
static long |
MILLIS_PER_SECOND
The number of milliseconds in a second.
|
static long |
NANOS_PER_MILLI
The number of nanoseconds in a millisecond.
|
static long |
SECONDS_PER_DAY
The number of seconds in a day.
|
static String |
TIME_FORMAT_STRING
The SimpleDateFormat string for ISO times, "HH:mm:ss".
|
static String |
TIMESTAMP_FORMAT_STRING
The SimpleDateFormat string for ISO timestamps, "yyyy-MM-dd HH:mm:ss".
|
static TimeZone |
UTC_ZONE
The UTC time zone.
|
static Calendar |
ZERO_CALENDAR
Calendar set to the epoch (1970-01-01 00:00:00 UTC).
|
Modifier and Type | Method and Description |
---|---|
static int |
addMonths(int date,
int m)
Adds a given number of months to a date, represented as the number of
days since the epoch.
|
static long |
addMonths(long timestamp,
int m)
Adds a given number of months to a timestamp, represented as the number
of milliseconds since the epoch.
|
static Calendar |
calendar()
Creates an instance of
Calendar in the root locale and UTC time
zone. |
static void |
checkDateFormat(String pattern)
Checks if the date/time format is valid
|
static int |
dateStringToUnixDate(String s) |
static int |
digitCount(int v) |
static long |
floorDiv(long x,
long y)
Deprecated.
|
static long |
floorMod(long x,
long y)
Deprecated.
|
static TimeZone |
getTimeZone(Calendar cal)
Gets the active time zone based on a Calendar argument
|
static String |
intervalDayTimeToString(long v,
TimeUnitRange range,
int scale) |
static String |
intervalYearMonthToString(int v,
TimeUnitRange range) |
static boolean |
isOffsetDateTime(Object o)
Returns whether a value is an
OffsetDateTime . |
static int |
lastDay(int date)
SQL
LAST_DAY function. |
static SimpleDateFormat |
newDateFormat(String format)
Creates a new date formatter with Farrago specific options.
|
static StringBuilder |
number(StringBuilder buf,
int v,
int n) |
static String |
offsetDateTimeValue(Object o)
Returns the value of a
OffsetDateTime as a string. |
static Calendar |
parseDateFormat(String s,
DateFormat dateFormat,
TimeZone tz)
Parses a string using
SimpleDateFormat and a given pattern. |
static Calendar |
parseDateFormat(String s,
String pattern,
TimeZone tz)
Deprecated.
|
static DateTimeUtils.PrecisionTime |
parsePrecisionDateTimeLiteral(String s,
DateFormat dateFormat,
TimeZone tz,
int maxPrecision)
Parses a string using
SimpleDateFormat and a given pattern, and
if present, parses a fractional seconds component. |
static DateTimeUtils.PrecisionTime |
parsePrecisionDateTimeLiteral(String s,
String pattern,
TimeZone tz)
Deprecated.
|
static long |
powerX(long a,
long b)
Cheap, unsafe, long power.
|
static long |
resetDate(long timestamp)
Resets to epoch (1970-01-01) the "date" part of a timestamp.
|
static long |
resetTime(long timestamp)
Resets to zero the "time" part of a timestamp.
|
static int |
sqlDateToUnixDate(Date date,
Calendar calendar)
Calculates the unix date as the number of days since January 1st, 1970 UTC for the given SQL
date.
|
static int |
sqlDateToUnixDate(Date date,
TimeZone timeZone)
Calculates the unix date as the number of days since January 1st, 1970 UTC for the given SQL
date.
|
static long |
sqlTimestampToUnixTimestamp(Timestamp timestamp,
Calendar calendar)
Calculates the unix date as the number of milliseconds since January 1st, 1970 UTC for the
given SQL timestamp.
|
static long |
sqlTimestampToUnixTimestamp(Timestamp timestamp,
TimeZone timeZone)
Calculates the unix date as the number of milliseconds since January 1st, 1970 UTC for the
given SQL timestamp.
|
static int |
sqlTimeToUnixTime(Time time,
Calendar calendar)
Calculates the unix time as the number of milliseconds since the previous day in UTC for the
given SQL time.
|
static int |
sqlTimeToUnixTime(Time time,
TimeZone timeZone)
Calculates the unix time as the number of milliseconds since the previous day in UTC for the
given SQL time.
|
static int |
subtractMonths(int date0,
int date1)
Finds the number of months between two dates, each represented as the
number of days since the epoch.
|
static int |
subtractMonths(long t0,
long t1) |
static long |
timestampStringToUnixDate(String s) |
static int |
timeStringToUnixDate(String v) |
static int |
timeStringToUnixDate(String v,
int start) |
static long |
unixDateCeil(TimeUnitRange range,
long date) |
static long |
unixDateExtract(TimeUnitRange range,
long date) |
static long |
unixDateFloor(TimeUnitRange range,
long date) |
static Date |
unixDateToSqlDate(int date,
Calendar calendar)
Converts the given unix date to a SQL date.
|
static String |
unixDateToString(int date)
Helper for CAST({date} AS VARCHAR(n)).
|
static int |
unixTimeExtract(TimeUnitRange range,
int time)
Extracts a time unit from a time value (milliseconds since midnight).
|
static long |
unixTimestamp(int year,
int month,
int day,
int hour,
int minute,
int second) |
static long |
unixTimestampCeil(TimeUnitRange range,
long timestamp) |
static int |
unixTimestampExtract(TimeUnitRange range,
long timestamp)
Extracts a time unit from a UNIX date (milliseconds since epoch).
|
static long |
unixTimestampFloor(TimeUnitRange range,
long timestamp) |
static Timestamp |
unixTimestampToSqlTimestamp(long timestamp,
Calendar calendar)
Converts the given unix timestamp to a SQL timestamp.
|
static String |
unixTimestampToString(long timestamp)
Helper for CAST({timestamp} AS VARCHAR(n)).
|
static String |
unixTimestampToString(long timestamp,
int precision) |
static Date |
unixTimestampToUtilDate(long timestamp,
Calendar calendar)
Converts the given unix timestamp to a Java date.
|
static Time |
unixTimeToSqlTime(int time,
Calendar calendar)
Converts the given unix time to a SQL time.
|
static String |
unixTimeToString(int time)
Helper for CAST({timestamp} AS VARCHAR(n)).
|
static String |
unixTimeToString(int time,
int precision) |
static long |
utilDateToUnixTimestamp(Date date,
Calendar calendar)
Calculates the unix date as the number of milliseconds since January 1st, 1970 UTC for the
given date.
|
static long |
utilDateToUnixTimestamp(Date date,
TimeZone timeZone)
Calculates the unix date as the number of milliseconds since January 1st, 1970 UTC for the
given date.
|
static int |
ymdToJulian(int year,
int month,
int day)
Calculates the Julian Day Number for any valid date in the Gregorian
calendar.
|
static int |
ymdToUnixDate(int year,
int month,
int day) |
public static final int EPOCH_JULIAN
public static final String DATE_FORMAT_STRING
public static final String TIME_FORMAT_STRING
public static final String TIMESTAMP_FORMAT_STRING
@Deprecated public static final TimeZone GMT_ZONE
UTC_ZONE
public static final TimeZone UTC_ZONE
public static final TimeZone DEFAULT_ZONE
public static final long MILLIS_PER_SECOND
public static final long MILLIS_PER_MINUTE
public static final long MILLIS_PER_HOUR
public static final long MILLIS_PER_DAY
This is the modulo 'mask' used when converting TIMESTAMP values to DATE and TIME values.
public static final long SECONDS_PER_DAY
public static final long NANOS_PER_MILLI
public static final Calendar ZERO_CALENDAR
@Deprecated public static Calendar parseDateFormat(String s, String pattern, TimeZone tz)
public static Calendar parseDateFormat(String s, DateFormat dateFormat, TimeZone tz)
SimpleDateFormat
and a given pattern. The
entire string must match the pattern specified.s
- string to be parseddateFormat
- Date formattz
- time zone in which to interpret string. Defaults to the Java
default time zone@Deprecated public static DateTimeUtils.PrecisionTime parsePrecisionDateTimeLiteral(String s, String pattern, TimeZone tz)
public static DateTimeUtils.PrecisionTime parsePrecisionDateTimeLiteral(String s, DateFormat dateFormat, TimeZone tz, int maxPrecision)
SimpleDateFormat
and a given pattern, and
if present, parses a fractional seconds component. The fractional seconds
component must begin with a decimal point ('.') followed by numeric
digits. The precision is rounded to a maximum of 3 digits of fractional
seconds precision (to obtain milliseconds).s
- string to be parseddateFormat
- Date formattz
- time zone in which to interpret string. Defaults to the
local time zonePrecisionTime
initialized
with the parsed value, or null if parsing failed. The PrecisionTime
contains a GMT Calendar and a precision.public static TimeZone getTimeZone(Calendar cal)
public static void checkDateFormat(String pattern)
pattern
- SimpleDateFormat
patternIllegalArgumentException
- if the given pattern is invalidpublic static SimpleDateFormat newDateFormat(String format)
format
- SimpleDateFormat
patternpublic static String unixTimestampToString(long timestamp)
public static String unixTimestampToString(long timestamp, int precision)
public static String unixTimeToString(int time)
public static String unixTimeToString(int time, int precision)
public static String unixDateToString(int date)
public static String intervalYearMonthToString(int v, TimeUnitRange range)
public static StringBuilder number(StringBuilder buf, int v, int n)
public static int digitCount(int v)
public static long powerX(long a, long b)
public static String intervalDayTimeToString(long v, TimeUnitRange range, int scale)
public static int dateStringToUnixDate(String s)
public static int timeStringToUnixDate(String v)
public static int timeStringToUnixDate(String v, int start)
public static long timestampStringToUnixDate(String s)
public static long unixDateExtract(TimeUnitRange range, long date)
public static int unixTimestampExtract(TimeUnitRange range, long timestamp)
public static int unixTimeExtract(TimeUnitRange range, int time)
public static long resetTime(long timestamp)
public static long resetDate(long timestamp)
public static long unixTimestampFloor(TimeUnitRange range, long timestamp)
public static long unixDateFloor(TimeUnitRange range, long date)
public static long unixTimestampCeil(TimeUnitRange range, long timestamp)
public static long unixDateCeil(TimeUnitRange range, long date)
public static int ymdToUnixDate(int year, int month, int day)
public static int ymdToJulian(int year, int month, int day)
If date is invalid, result is unspecified.
See an explanation of this algorithm.
year
- Year (e.g. 2020 means 2020 CE, 1 means 1 CE, 0 means 1 BCE
because there is no 0 CE, -1 means 2 BCE, etc.)month
- Month (between 1 and 12 inclusive, 1 meaning January)day
- Day of month (between 1 and 31 inclusive)public static long unixTimestamp(int year, int month, int day, int hour, int minute, int second)
public static long addMonths(long timestamp, int m)
public static int addMonths(int date, int m)
public static int lastDay(int date)
LAST_DAY
function.date
- days since epochpublic static int subtractMonths(int date0, int date1)
public static int subtractMonths(long t0, long t1)
@Deprecated public static long floorDiv(long x, long y)
Math.floorDiv(long, long)
@Deprecated public static long floorMod(long x, long y)
Math.floorMod(long, long)
public static Calendar calendar()
Calendar
in the root locale and UTC time
zone.public static boolean isOffsetDateTime(Object o)
OffsetDateTime
.public static String offsetDateTimeValue(Object o)
OffsetDateTime
as a string.public static int sqlDateToUnixDate(Date date, Calendar calendar)
public static int sqlDateToUnixDate(Date date, TimeZone timeZone)
The Date
class uses the standard Gregorian calendar which switches from
the Julian calendar to the Gregorian calendar in October 1582. For compatibility with
ISO-8601, the value is converted to a LocalDate
which uses the proleptic Gregorian
calendar.
For backwards compatibility, timezone offsets are calculated in relation to the local
timezone instead of UTC. Providing the default timezone or null
will return the date
unmodified.
If the date contains a partial day, it will be rounded to a full day depending on the milliseconds value. If the milliseconds value is positive, it will be rounded down to the closest full day. If the milliseconds value is negative, it will be rounded up to the closest full day.
public static Date unixDateToSqlDate(int date, Calendar calendar)
The unix date should be the number of days since January 1st, 1970 UTC using the proleptic
Gregorian calendar as defined by ISO-8601. The returned Date
object will use
the standard Gregorian calendar which switches from the Julian calendar to the Gregorian
calendar in October 1582. This conversion is handled by the Date
class when
converting from a LocalDate
object.
For backwards compatibility, timezone offsets are calculated in relation to the local
timezone instead of UTC. Providing the default timezone or null
will return the date
unmodified.
public static long utilDateToUnixTimestamp(Date date, Calendar calendar)
utilDateToUnixTimestamp(Date, TimeZone)
public static long utilDateToUnixTimestamp(Date date, TimeZone timeZone)
The Date
class uses the standard Gregorian calendar which switches from the Julian
calendar to the Gregorian calendar in October 1582. For compatibility with ISO-8601, the value
is converted to a java.time
object which uses the proleptic Gregorian calendar.
For backwards compatibility, timezone offsets are calculated in relation to the local
timezone instead of UTC. Providing the default timezone or null
will return the date
unmodified.
public static Date unixTimestampToUtilDate(long timestamp, Calendar calendar)
The unix timestamp should be the number of milliseconds since January 1st, 1970 UTC using
the proleptic Gregorian calendar as defined by ISO-8601. The returned Date
object will
use the standard Gregorian calendar which switches from the Julian calendar to the Gregorian
calendar in October 1582. This conversion is handled by the java.sql
package when
converting from a java.time
object.
For backwards compatibility, timezone offsets are calculated in relation to the local
timezone instead of UTC. Providing the default timezone or null
will return the date
unmodified.
public static int sqlTimeToUnixTime(Time time, Calendar calendar)
sqlTimeToUnixTime(Time, TimeZone)
public static int sqlTimeToUnixTime(Time time, TimeZone timeZone)
For backwards compatibility, timezone offsets are calculated in relation to the local
timezone instead of UTC. Providing the default timezone or null
will return the time
unmodified.
public static Time unixTimeToSqlTime(int time, Calendar calendar)
The unix time should be the number of milliseconds since the previous day in UTC.
For backwards compatibility, timezone offsets are calculated in relation to the local
timezone instead of UTC. Providing the default timezone or null
will return the time
unmodified.
public static long sqlTimestampToUnixTimestamp(Timestamp timestamp, Calendar calendar)
public static long sqlTimestampToUnixTimestamp(Timestamp timestamp, TimeZone timeZone)
The Timestamp
class uses the standard Gregorian calendar which switches from the
Julian calendar to the Gregorian calendar in October 1582. For compatibility with ISO-8601,
the value is converted to a LocalDateTime
which uses the proleptic Gregorian calendar.
For backwards compatibility, timezone offsets are calculated in relation to the local
timezone instead of UTC. Providing the default timezone or null
will return the
timestamp unmodified.
public static Timestamp unixTimestampToSqlTimestamp(long timestamp, Calendar calendar)
The unix timestamp should be the number of milliseconds since January 1st, 1970 UTC using
the proleptic Gregorian calendar as defined by ISO-8601. The returned Timestamp
object
will use the standard Gregorian calendar which switches from the Julian calendar to the
Gregorian calendar in October 1582. This conversion is handled by the Timestamp
class
when converting from a LocalDateTime
object.
For backwards compatibility, timezone offsets are calculated in relation to the local
timezone instead of UTC. Providing the default timezone or null
will return the
timestamp unmodified.
Copyright © 2012-2024 Apache Software Foundation. All Rights Reserved.