@Internal public class DateTimeUtils extends Object
These utils include:
parse[type]
: methods for parsing strings to date/time/timestamp
format[type]
: methods for formatting date/time/timestamp
to[externalTypeName]
and toInternal
: methods for converting values from
internal date/time/timestamp types from/to java.sql or java.time types
DateTimeUtils.TimeUnit
and DateTimeUtils.TimeUnitRange
enums
Currently, this class is a bit messy because it includes a mix of functionalities both from
common and planner. We should strive to reduce the number of functionalities here, eventually
moving some methods closer to where they're needed. Connectors and formats should not use this
class, but rather if a functionality is necessary, it should be part of the public APIs of our
type system (e.g a new method in TimestampData
or in TimestampType
). Methods used
only by the planner should live inside the planner whenever is possible.
Modifier and Type | Class and Description |
---|---|
static class |
DateTimeUtils.TimeUnit
Enumeration of time units used to construct an interval.
|
static class |
DateTimeUtils.TimeUnitRange
A range of time units.
|
Modifier and Type | Field and Description |
---|---|
static int |
EPOCH_JULIAN
The julian date of the epoch, 1970-01-01.
|
static TimeZone |
LOCAL_TZ
The local time zone.
|
static long |
MILLIS_PER_DAY
The number of milliseconds in a day.
|
static TimeZone |
UTC_ZONE
The UTC time zone.
|
Constructor and Description |
---|
DateTimeUtils() |
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 String |
convertTz(String dateStr,
String tzFrom,
String tzTo)
Convert datetime string from a time zone to another time zone.
|
static TimestampData |
dateToTimestampWithLocalZone(int date,
TimeZone tz) |
static long |
extractFromDate(DateTimeUtils.TimeUnitRange range,
int date) |
static long |
extractFromDate(DateTimeUtils.TimeUnitRange range,
long date) |
static long |
extractFromTimestamp(DateTimeUtils.TimeUnitRange range,
TimestampData ts,
TimeZone tz) |
static String |
formatDate(int date)
Helper for CAST({date} AS VARCHAR(n)).
|
static String |
formatIntervalDayTime(long v) |
static String |
formatIntervalYearMonth(int v) |
static String |
formatTimestamp(TimestampData ts,
String format) |
static String |
formatTimestamp(TimestampData ts,
String format,
TimeZone zone) |
static String |
formatTimestamp(TimestampData ts,
TimeZone tz,
int precision) |
static String |
formatTimestampMillis(int time,
int precision) |
static String |
formatTimestampMillis(long ts,
String format,
TimeZone tz) |
static String |
formatTimestampString(String dateStr,
String toFormat) |
static String |
formatTimestampString(String dateStr,
String fromFormat,
String toFormat,
TimeZone tz) |
static String |
formatTimestampString(String dateStr,
String toFormat,
TimeZone tz) |
static String |
formatUnixTimestamp(long unixtime,
String format,
TimeZone tz)
Convert unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to datetime string in the
given format.
|
static String |
formatUnixTimestamp(long unixtime,
TimeZone tz)
Convert unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to datetime string in the
"yyyy-MM-dd HH:mm:ss" format.
|
static long |
fromTimestamp(long ts) |
static StringBuilder |
number(StringBuilder buf,
int v,
int n) |
static Integer |
parseDate(String s) |
static int |
parseDate(String dateStr,
String fromFormat)
Returns the epoch days since 1970-01-01.
|
static Integer |
parseTime(String v) |
static TimestampData |
parseTimestampData(String dateStr) |
static TimestampData |
parseTimestampData(String dateStr,
int precision) |
static TimestampData |
parseTimestampData(String dateStr,
int precision,
TimeZone timeZone) |
static TimestampData |
parseTimestampData(String dateStr,
String format) |
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 |
timestampCeil(DateTimeUtils.TimeUnitRange range,
long ts,
TimeZone tz)
Keep the algorithm consistent with Calcite DateTimeUtils.julianDateFloor, but here we take
time zone into account.
|
static long |
timestampFloor(DateTimeUtils.TimeUnitRange range,
long ts,
TimeZone tz) |
static int |
timestampMillisToDate(long ts)
Get date from a timestamp.
|
static int |
timestampMillisToTime(long ts)
Get time from a timestamp.
|
static TimestampData |
timestampToTimestampWithLocalZone(TimestampData ts,
TimeZone tz) |
static int |
timestampWithLocalZoneToDate(TimestampData ts,
TimeZone tz) |
static int |
timestampWithLocalZoneToTime(TimestampData ts,
TimeZone tz) |
static TimestampData |
timestampWithLocalZoneToTimestamp(TimestampData ts,
TimeZone tz) |
static TimestampData |
timeToTimestampWithLocalZone(int time,
TimeZone tz) |
static int |
toInternal(Date date)
Converts the Java type used for UDF parameters of SQL DATE type (
Date ) to
internal representation (int). |
static int |
toInternal(LocalDate date) |
static int |
toInternal(LocalTime time) |
static int |
toInternal(Time time)
Converts the Java type used for UDF parameters of SQL TIME type (
Time ) to
internal representation (int). |
static long |
toInternal(Timestamp ts)
Converts the Java type used for UDF parameters of SQL TIMESTAMP type (
Timestamp ) to internal representation (long). |
static LocalDate |
toLocalDate(int date) |
static LocalDateTime |
toLocalDateTime(long timestamp) |
static LocalTime |
toLocalTime(int time) |
static Date |
toSQLDate(int v)
Converts the internal representation of a SQL DATE (int) to the Java type used for UDF
parameters (
Date ). |
static Time |
toSQLTime(int v)
Converts the internal representation of a SQL TIME (int) to the Java type used for UDF
parameters (
Time ). |
static Timestamp |
toSQLTimestamp(long v)
Converts the internal representation of a SQL TIMESTAMP (long) to the Java type used for UDF
parameters (
Timestamp ). |
static TimestampData |
toTimestampData(DecimalData v,
int precision) |
static TimestampData |
toTimestampData(double v,
int precision) |
static TimestampData |
toTimestampData(long v,
int precision) |
static long |
toTimestampMillis(LocalDateTime dateTime) |
static TimestampData |
truncate(TimestampData ts,
int precision) |
static long |
unixDateCeil(DateTimeUtils.TimeUnitRange range,
long date) |
static long |
unixDateFloor(DateTimeUtils.TimeUnitRange range,
long date) |
static long |
unixTimestamp()
Returns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an unsigned integer.
|
static long |
unixTimestamp(long ts)
Returns the value of the timestamp to seconds since '1970-01-01 00:00:00' UTC.
|
static long |
unixTimestamp(String dateStr,
String format,
TimeZone tz)
Returns the value of the argument as an unsigned integer in seconds since '1970-01-01
00:00:00' UTC.
|
static long |
unixTimestamp(String dateStr,
TimeZone tz)
Returns the value of the argument as an unsigned integer in seconds since '1970-01-01
00:00:00' UTC.
|
static long |
unixTimestampCeil(DateTimeUtils.TimeUnitRange range,
long timestamp) |
static long |
unixTimestampFloor(DateTimeUtils.TimeUnitRange range,
long timestamp) |
public static final int EPOCH_JULIAN
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 TimeZone UTC_ZONE
public static final TimeZone LOCAL_TZ
public static Date toSQLDate(int v)
Date
).public static Time toSQLTime(int v)
Time
).public static Timestamp toSQLTimestamp(long v)
Timestamp
).public static int toInternal(Date date)
Date
) to
internal representation (int).
Converse of toSQLDate(int)
.
public static int toInternal(Time time)
Time
) to
internal representation (int).
Converse of toSQLTime(int)
.
public static long toInternal(Timestamp ts)
Timestamp
) to internal representation (long).
Converse of toSQLTimestamp(long)
.
public static LocalDate toLocalDate(int date)
public static int toInternal(LocalDate date)
public static LocalTime toLocalTime(int time)
public static int toInternal(LocalTime time)
public static LocalDateTime toLocalDateTime(long timestamp)
public static long toTimestampMillis(LocalDateTime dateTime)
public static TimestampData toTimestampData(long v, int precision)
public static TimestampData toTimestampData(double v, int precision)
public static TimestampData toTimestampData(DecimalData v, int precision)
public static TimestampData parseTimestampData(String dateStr) throws DateTimeException
DateTimeException
public static TimestampData parseTimestampData(String dateStr, int precision) throws DateTimeException
DateTimeException
public static TimestampData parseTimestampData(String dateStr, int precision, TimeZone timeZone) throws DateTimeException
DateTimeException
public static TimestampData parseTimestampData(String dateStr, String format)
public static int parseDate(String dateStr, String fromFormat)
public static String formatTimestamp(TimestampData ts, String format)
public static String formatTimestamp(TimestampData ts, String format, TimeZone zone)
public static String formatTimestamp(TimestampData ts, TimeZone tz, int precision)
public static String formatTimestampMillis(long ts, String format, TimeZone tz)
public static String formatTimestampString(String dateStr, String fromFormat, String toFormat, TimeZone tz)
public static String formatTimestampString(String dateStr, String toFormat, TimeZone tz)
public static String formatTimestampMillis(int time, int precision)
public static String formatDate(int date)
public static String formatIntervalYearMonth(int v)
public static StringBuilder number(StringBuilder buf, int v, int n)
public static String formatIntervalDayTime(long v)
public static long extractFromTimestamp(DateTimeUtils.TimeUnitRange range, TimestampData ts, TimeZone tz)
public static long extractFromDate(DateTimeUtils.TimeUnitRange range, long date)
public static long extractFromDate(DateTimeUtils.TimeUnitRange range, int date)
public static long timestampFloor(DateTimeUtils.TimeUnitRange range, long ts, TimeZone tz)
public static long timestampCeil(DateTimeUtils.TimeUnitRange range, long ts, TimeZone tz)
public static String convertTz(String dateStr, String tzFrom, String tzTo)
dateStr
- the date time stringtzFrom
- the original time zonetzTo
- the target time zonepublic static int timestampMillisToDate(long ts)
ts
- the timestamp in milliseconds.public static int timestampMillisToTime(long ts)
ts
- the timestamp in milliseconds.public static long fromTimestamp(long ts)
public static String formatUnixTimestamp(long unixtime, TimeZone tz)
public static String formatUnixTimestamp(long unixtime, String format, TimeZone tz)
public static long unixTimestamp()
public static long unixTimestamp(long ts)
public static long unixTimestamp(String dateStr, TimeZone tz)
public static long unixTimestamp(String dateStr, String format, TimeZone tz)
public static TimestampData timestampToTimestampWithLocalZone(TimestampData ts, TimeZone tz)
public static TimestampData timestampWithLocalZoneToTimestamp(TimestampData ts, TimeZone tz)
public static int timestampWithLocalZoneToDate(TimestampData ts, TimeZone tz)
public static int timestampWithLocalZoneToTime(TimestampData ts, TimeZone tz)
public static TimestampData dateToTimestampWithLocalZone(int date, TimeZone tz)
public static TimestampData timeToTimestampWithLocalZone(int time, TimeZone tz)
public static TimestampData truncate(TimestampData ts, int precision)
public static long unixDateCeil(DateTimeUtils.TimeUnitRange range, long date)
public static long unixDateFloor(DateTimeUtils.TimeUnitRange range, long date)
public static long unixTimestampFloor(DateTimeUtils.TimeUnitRange range, long timestamp)
public static long unixTimestampCeil(DateTimeUtils.TimeUnitRange range, long timestamp)
public static long addMonths(long timestamp, int m)
public static int addMonths(int date, int m)
public static int subtractMonths(int date0, int date1)
public static int subtractMonths(long t0, long t1)
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.