Package org.apache.flink.table.utils
Class DateTimeUtils
- java.lang.Object
-
- org.apache.flink.table.utils.DateTimeUtils
-
@Internal public class DateTimeUtils extends Object
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
TimestampDataor inTimestampType). Methods used only by the planner should live inside the planner whenever is possible.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDateTimeUtils.TimeUnitEnumeration of time units used to construct an interval.static classDateTimeUtils.TimeUnitRangeA range of time units.
-
Field Summary
Fields Modifier and Type Field Description static intEPOCH_JULIANThe julian date of the epoch, 1970-01-01.static TimeZoneLOCAL_TZThe local time zone.static longMILLIS_PER_DAYThe number of milliseconds in a day.static TimeZoneUTC_ZONEThe UTC time zone.
-
Constructor Summary
Constructors Constructor Description DateTimeUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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 StringconvertTz(String dateStr, String tzFrom, String tzTo)Convert 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 IntegerparseDate(String s)static intparseDate(String dateStr, String fromFormat)Returns the epoch days since 1970-01-01.static IntegerparseTime(String v)static 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 TimestampDatatimestampToTimestampWithLocalZone(TimestampData ts, TimeZone tz)static inttimestampWithLocalZoneToDate(TimestampData ts, TimeZone tz)static inttimestampWithLocalZoneToTime(TimestampData ts, TimeZone tz)static TimestampDatatimestampWithLocalZoneToTimestamp(TimestampData ts, TimeZone tz)static 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 longunixTimestamp()Returns 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 Detail
-
EPOCH_JULIAN
public static final int EPOCH_JULIAN
The julian date of the epoch, 1970-01-01.- See Also:
- Constant Field Values
-
MILLIS_PER_DAY
public static final long MILLIS_PER_DAY
The number of milliseconds in a day.This is the modulo 'mask' used when converting TIMESTAMP values to DATE and TIME values.
- See Also:
- Constant Field Values
-
UTC_ZONE
public static final TimeZone UTC_ZONE
The UTC time zone.
-
LOCAL_TZ
public static final TimeZone LOCAL_TZ
The local time zone.
-
-
Method Detail
-
toSQLDate
public static Date toSQLDate(int v)
Converts the internal representation of a SQL DATE (int) to the Java type used for UDF parameters (Date).
-
toSQLTime
public static Time toSQLTime(int v)
Converts the internal representation of a SQL TIME (int) to the Java type used for UDF parameters (Time).
-
toSQLTimestamp
public static Timestamp toSQLTimestamp(long v)
Converts the internal representation of a SQL TIMESTAMP (long) to the Java type used for UDF parameters (Timestamp).
-
toInternal
public static int toInternal(Date date)
Converts the Java type used for UDF parameters of SQL DATE type (Date) to internal representation (int).Converse of
toSQLDate(int).
-
toInternal
public static int toInternal(Time time)
Converts the Java type used for UDF parameters of SQL TIME type (Time) to internal representation (int).Converse of
toSQLTime(int).
-
toInternal
public static long toInternal(Timestamp ts)
Converts the Java type used for UDF parameters of SQL TIMESTAMP type (Timestamp) to internal representation (long).Converse of
toSQLTimestamp(long).
-
toLocalDate
public static LocalDate toLocalDate(int date)
-
toInternal
public static int toInternal(LocalDate date)
-
toLocalTime
public static LocalTime toLocalTime(int time)
-
toInternal
public static int toInternal(LocalTime time)
-
toLocalDateTime
public static LocalDateTime toLocalDateTime(long timestamp)
-
toTimestampMillis
public static long toTimestampMillis(LocalDateTime dateTime)
-
toTimestampData
public static TimestampData toTimestampData(long v, int precision)
-
toTimestampData
public static TimestampData toTimestampData(double v, int precision)
-
toTimestampData
public static TimestampData toTimestampData(DecimalData v, int precision)
-
parseTimestampData
public static TimestampData parseTimestampData(String dateStr) throws DateTimeException
- 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
public static TimestampData parseTimestampData(String dateStr, String format)
-
parseDate
public static int parseDate(String dateStr, String fromFormat)
Returns the epoch days since 1970-01-01.
-
formatTimestamp
public static String formatTimestamp(TimestampData ts, String format)
-
formatTimestamp
public static String formatTimestamp(TimestampData ts, String format, TimeZone zone)
-
formatTimestamp
public static String formatTimestamp(TimestampData ts, TimeZone tz, int precision)
-
formatTimestampMillis
public static String formatTimestampMillis(long ts, String format, TimeZone tz)
-
formatTimestampString
public static String formatTimestampString(String dateStr, String fromFormat, String toFormat, TimeZone tz)
-
formatTimestampStringWithOffset
public static String formatTimestampStringWithOffset(String dateStr, String fromFormat, String toFormat, TimeZone tz, long offsetMills)
-
formatTimestampString
public static String formatTimestampString(String dateStr, String toFormat, TimeZone tz)
-
formatTimestampMillis
public static String formatTimestampMillis(int time, int precision)
-
formatDate
public static String formatDate(int date)
Helper for CAST({date} AS VARCHAR(n)).
-
formatIntervalYearMonth
public static String formatIntervalYearMonth(int v)
-
number
public static StringBuilder number(StringBuilder buf, int v, int n)
-
formatIntervalDayTime
public static String formatIntervalDayTime(long v)
-
extractFromTimestamp
public static long extractFromTimestamp(DateTimeUtils.TimeUnitRange range, TimestampData ts, TimeZone tz)
-
extractFromDate
public static long extractFromDate(DateTimeUtils.TimeUnitRange range, long date)
-
extractFromDate
public static long extractFromDate(DateTimeUtils.TimeUnitRange range, int date)
-
timestampFloor
public static long timestampFloor(DateTimeUtils.TimeUnitRange range, long ts, TimeZone tz)
-
timestampCeil
public 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.
-
convertTz
public static String convertTz(String dateStr, String tzFrom, String tzTo)
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
public 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.
-
formatUnixTimestamp
public 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.
-
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
public 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.
-
unixTimestamp
public 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.
-
timestampToTimestampWithLocalZone
public static TimestampData timestampToTimestampWithLocalZone(TimestampData ts, TimeZone tz)
-
timestampWithLocalZoneToTimestamp
public static TimestampData timestampWithLocalZoneToTimestamp(TimestampData ts, TimeZone tz)
-
timestampWithLocalZoneToDate
public static int timestampWithLocalZoneToDate(TimestampData ts, TimeZone tz)
-
timestampWithLocalZoneToTime
public static int timestampWithLocalZoneToTime(TimestampData ts, TimeZone tz)
-
dateToTimestampWithLocalZone
public static TimestampData dateToTimestampWithLocalZone(int date, TimeZone tz)
-
timeToTimestampWithLocalZone
public static TimestampData timeToTimestampWithLocalZone(int time, TimeZone tz)
-
truncate
public static TimestampData truncate(TimestampData ts, int precision)
-
unixDateCeil
public static long unixDateCeil(DateTimeUtils.TimeUnitRange range, long date)
-
unixDateFloor
public static long unixDateFloor(DateTimeUtils.TimeUnitRange range, long date)
-
unixTimestampFloor
public static long unixTimestampFloor(DateTimeUtils.TimeUnitRange range, long timestamp)
-
unixTimestampCeil
public static long unixTimestampCeil(DateTimeUtils.TimeUnitRange range, long timestamp)
-
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)
-
-