Package org.apache.flink.table.utils
Class DateTimeUtils
java.lang.Object
org.apache.flink.table.utils.DateTimeUtils
Utility functions for datetime types: date, time, timestamp.
These utils include:
parse[type]: methods for parsing strings to date/time/timestampformat[type]: methods for formatting date/time/timestampto[externalTypeName]andtoInternal: methods for converting values from internal date/time/timestamp types from/to java.sql or java.time types- Various operations on timestamp, including floor, ceil and extract
DateTimeUtils.TimeUnitandDateTimeUtils.TimeUnitRangeenums
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration of time units used to construct an interval.static enumA range of time units. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intaddMonths(int date, int m) Adds a given number of months to a date, represented as the number of days since the epoch.static longaddMonths(long timestamp, int m) Adds a given number of months to a timestamp, represented as the number of milliseconds since the epoch.static StringConvert datetime string from a time zone to another time zone.static TimestampDatadateToTimestampWithLocalZone(int date, TimeZone tz) static longextractFromDate(DateTimeUtils.TimeUnitRange range, int date) static longextractFromDate(DateTimeUtils.TimeUnitRange range, long date) static longextractFromTimestamp(DateTimeUtils.TimeUnitRange range, TimestampData ts, TimeZone tz) static StringformatDate(int date) Helper for CAST({date} AS VARCHAR(n)).static StringformatIntervalDayTime(long v) static StringformatIntervalYearMonth(int v) static StringformatTimestamp(TimestampData ts, String format) static StringformatTimestamp(TimestampData ts, String format, TimeZone zone) static StringformatTimestamp(TimestampData ts, TimeZone tz, int precision) static StringformatTimestampMillis(int time, int precision) static StringformatTimestampMillis(long ts, String format, TimeZone tz) static StringformatTimestampString(String dateStr, String toFormat) static StringformatTimestampString(String dateStr, String fromFormat, String toFormat, TimeZone tz) static StringformatTimestampString(String dateStr, String toFormat, TimeZone tz) static StringformatTimestampStringWithOffset(String dateStr, String fromFormat, String toFormat, TimeZone tz, long offsetMills) static StringformatUnixTimestamp(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 StringformatUnixTimestamp(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 longfromTimestamp(long ts) static StringBuildernumber(StringBuilder buf, int v, int n) static Integerstatic intReturns the epoch days since 1970-01-01.static Integerstatic TimestampDataparseTimestampData(String dateStr) static TimestampDataparseTimestampData(String dateStr, int precision) static TimestampDataparseTimestampData(String dateStr, int precision, TimeZone timeZone) static TimestampDataparseTimestampData(String dateStr, String format) static intsubtractMonths(int date0, int date1) Finds the number of months between two dates, each represented as the number of days since the epoch.static intsubtractMonths(long t0, long t1) static longtimestampCeil(DateTimeUtils.TimeUnitRange range, long ts, TimeZone tz) Keep the algorithm consistent with Calcite DateTimeUtils.julianDateFloor, but here we take time zone into account.static longtimestampFloor(DateTimeUtils.TimeUnitRange range, long ts, TimeZone tz) static inttimestampMillisToDate(long ts) Get date from a timestamp.static inttimestampMillisToTime(long ts) Get time from a timestamp.static TimestampDatastatic intstatic intstatic TimestampDatastatic TimestampDatatimeToTimestampWithLocalZone(int time, TimeZone tz) static inttoInternal(Date date) Converts the Java type used for UDF parameters of SQL DATE type (Date) to internal representation (int).static inttoInternal(Time time) Converts the Java type used for UDF parameters of SQL TIME type (Time) to internal representation (int).static longtoInternal(Timestamp ts) Converts the Java type used for UDF parameters of SQL TIMESTAMP type (Timestamp) to internal representation (long).static inttoInternal(LocalDate date) static inttoInternal(LocalTime time) static LocalDatetoLocalDate(int date) static LocalDateTimetoLocalDateTime(long timestamp) static LocalTimetoLocalTime(int time) static DatetoSQLDate(int v) Converts the internal representation of a SQL DATE (int) to the Java type used for UDF parameters (Date).static TimetoSQLTime(int v) Converts the internal representation of a SQL TIME (int) to the Java type used for UDF parameters (Time).static TimestamptoSQLTimestamp(long v) Converts the internal representation of a SQL TIMESTAMP (long) to the Java type used for UDF parameters (Timestamp).static TimestampDatatoTimestampData(double v, int precision) static TimestampDatatoTimestampData(long v, int precision) static TimestampDatatoTimestampData(DecimalData v, int precision) static longtoTimestampMillis(LocalDateTime dateTime) static TimestampDatatruncate(TimestampData ts, int precision) static longunixDateCeil(DateTimeUtils.TimeUnitRange range, long date) static longunixDateFloor(DateTimeUtils.TimeUnitRange range, long date) static longReturns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an unsigned integer.static longunixTimestamp(long ts) Returns the value of the timestamp to seconds since '1970-01-01 00:00:00' UTC.static longunixTimestamp(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 longunixTimestamp(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 longunixTimestampCeil(DateTimeUtils.TimeUnitRange range, long timestamp) static longunixTimestampFloor(DateTimeUtils.TimeUnitRange range, long timestamp)
-
Field Details
-
EPOCH_JULIAN
public static final int EPOCH_JULIANThe julian date of the epoch, 1970-01-01.- See Also:
-
MILLIS_PER_DAY
public static final long MILLIS_PER_DAYThe number of milliseconds in a day.This is the modulo 'mask' used when converting TIMESTAMP values to DATE and TIME values.
- See Also:
-
UTC_ZONE
The UTC time zone. -
LOCAL_TZ
The local time zone.
-
-
Constructor Details
-
DateTimeUtils
public DateTimeUtils()
-
-
Method Details
-
toSQLDate
Converts the internal representation of a SQL DATE (int) to the Java type used for UDF parameters (Date). -
toSQLTime
Converts the internal representation of a SQL TIME (int) to the Java type used for UDF parameters (Time). -
toSQLTimestamp
Converts the internal representation of a SQL TIMESTAMP (long) to the Java type used for UDF parameters (Timestamp). -
toInternal
Converts the Java type used for UDF parameters of SQL DATE type (Date) to internal representation (int).Converse of
toSQLDate(int). -
toInternal
Converts the Java type used for UDF parameters of SQL TIME type (Time) to internal representation (int).Converse of
toSQLTime(int). -
toInternal
Converts the Java type used for UDF parameters of SQL TIMESTAMP type (Timestamp) to internal representation (long).Converse of
toSQLTimestamp(long). -
toLocalDate
-
toInternal
-
toLocalTime
-
toInternal
-
toLocalDateTime
-
toTimestampMillis
-
toTimestampData
-
toTimestampData
-
toTimestampData
-
parseTimestampData
- Throws:
DateTimeException
-
parseTimestampData
public static TimestampData parseTimestampData(String dateStr, int precision) throws DateTimeException - Throws:
DateTimeException
-
parseTimestampData
public static TimestampData parseTimestampData(String dateStr, int precision, TimeZone timeZone) throws DateTimeException - Throws:
DateTimeException
-
parseTimestampData
-
parseDate
Returns the epoch days since 1970-01-01. -
parseDate
-
parseTime
-
formatTimestamp
-
formatTimestamp
-
formatTimestamp
-
formatTimestampMillis
-
formatTimestampString
-
formatTimestampStringWithOffset
-
formatTimestampString
-
formatTimestampString
-
formatTimestampMillis
-
formatDate
Helper for CAST({date} AS VARCHAR(n)). -
formatIntervalYearMonth
-
number
-
formatIntervalDayTime
-
extractFromTimestamp
public static long extractFromTimestamp(DateTimeUtils.TimeUnitRange range, TimestampData ts, TimeZone tz) -
extractFromDate
-
extractFromDate
-
timestampFloor
-
timestampCeil
Keep the algorithm consistent with Calcite DateTimeUtils.julianDateFloor, but here we take time zone into account. -
convertTz
Convert datetime string from a time zone to another time zone.- Parameters:
dateStr- the date time stringtzFrom- the original time zonetzTo- the target time zone
-
timestampMillisToDate
public static int timestampMillisToDate(long ts) Get date from a timestamp.- Parameters:
ts- the timestamp in milliseconds.- Returns:
- the date in days.
-
timestampMillisToTime
public static int timestampMillisToTime(long ts) Get time from a timestamp.- Parameters:
ts- the timestamp in milliseconds.- Returns:
- the time in milliseconds.
-
fromTimestamp
public static long fromTimestamp(long ts) -
formatUnixTimestamp
Convert unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to datetime string in the "yyyy-MM-dd HH:mm:ss" format. -
formatUnixTimestamp
Convert unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to datetime string in the given format. -
unixTimestamp
public static long unixTimestamp()Returns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an unsigned integer. -
unixTimestamp
public static long unixTimestamp(long ts) Returns the value of the timestamp to seconds since '1970-01-01 00:00:00' UTC. -
unixTimestamp
Returns the value of the argument as an unsigned integer in seconds since '1970-01-01 00:00:00' UTC. -
unixTimestamp
Returns the value of the argument as an unsigned integer in seconds since '1970-01-01 00:00:00' UTC. -
timestampToTimestampWithLocalZone
-
timestampWithLocalZoneToTimestamp
-
timestampWithLocalZoneToDate
-
timestampWithLocalZoneToTime
-
dateToTimestampWithLocalZone
-
timeToTimestampWithLocalZone
-
truncate
-
unixDateCeil
-
unixDateFloor
-
unixTimestampFloor
-
unixTimestampCeil
-
addMonths
public 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. -
addMonths
public 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. -
subtractMonths
public 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. -
subtractMonths
public static int subtractMonths(long t0, long t1)
-