Class DataTypeUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic DateTimeFormatterFormatter for the Date type.static final DateTimeFormatterstatic DateTimeFormatterFormatter for the DateTime type.static DateTimeFormatterFormatter for the DateTime type with nanoseconds.static final DateTimeFormatterstatic final DateTimeFormatter -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringdurationToTimeString(Duration duration, int precision) Converts aDurationto a time string in the format[-]HH:mm:ss[.nnnnnnnnn].static StringformatInstant(Instant instant) Formats anInstantobject for use in SQL statements or as query parameter.static StringformatInstant(Instant instant, ClickHouseDataType dataTypeHint) Formats anInstantobject for use in SQL statements or as query parameter.static StringformatInstant(Instant instant, ClickHouseDataType dataTypeHint, ZoneId timeZone) Formats anInstantobject for use in SQL statements or as query parameter.static InstantinstantFromTime64Integer(int precision, long value) static DurationlocalDateTimeToDuration(LocalDateTime localDateTime) static LocalDateTimelocalTimeFromTime64Integer(int precision, long value) static LocalDatetoLocalDate(Date sqlDate, TimeZone timeZone) static LocalDateTimetoLocalDateTime(Timestamp sqlTimestamp, TimeZone timeZone) Converts aTimestamptoLocalDateTimeusing the specified timezone.static LocalTimetoLocalTime(Time sqlTime, TimeZone timeZone) static Datestatic Timestatic TimestamptoSqlTimestamp(LocalDateTime localDateTime, TimeZone timeZone) Converts aLocalDateTimetoTimestampusing the specified timezone.static StringtoUnixTimestampString(long seconds, int nanos) static StringtoUnixTimestampString(Instant instant) Returns Unix Timestamp in nanoseconds as stringstatic StringtoUnixTimestampString(LocalDateTime localTs, TimeZone localTz) Returns Unix Timestamp in nanoseconds as stringstatic ZonedDateTimetoZonedDateTime(Timestamp sqlTimestamp, TimeZone timeZone) Converts aTimestamptoZonedDateTimeby expressing the timestamp's instant in the specified timezone.
-
Field Details
-
DATETIME_FORMATTER
Formatter for the DateTime type. -
DATE_FORMATTER
Formatter for the Date type. -
DATETIME_WITH_NANOS_FORMATTER
Formatter for the DateTime type with nanoseconds. -
TIME_WITH_NANOS_FORMATTER
-
TIME_FORMATTER
-
DATE_TIME_WITH_OPTIONAL_NANOS
-
-
Constructor Details
-
DataTypeUtils
public DataTypeUtils()
-
-
Method Details
-
formatInstant
Formats anInstantobject for use in SQL statements or as query parameter.- Parameters:
instant- the Java object to format- Returns:
- a suitable String representation of
instant - Throws:
NullPointerException- ifinstantis null
-
formatInstant
Formats anInstantobject for use in SQL statements or as query parameter. This method uses thedataTypeHintparameter to find the best suitable format for the instant.- Parameters:
instant- the Java object to formatdataTypeHint- the ClickHouse data typeinstantshould be used for- Returns:
- a suitable String representation of
instant - Throws:
NullPointerException- ifinstantis null
-
formatInstant
public static String formatInstant(Instant instant, ClickHouseDataType dataTypeHint, ZoneId timeZone) Formats anInstantobject for use in SQL statements or as query parameter. This method uses thedataTypeHintparameter to find the best suitable format for the instant. For some formatting operations, providing atimeZoneis mandatory, e.g. forClickHouseDataType.Date.- Parameters:
instant- the Java object to formatdataTypeHint- the ClickHouse data typeobjectshould be used fortimeZone- the time zone to be used when formatting the instant for use in non-time-zone-based ClickHouse data types- Returns:
- a suitable String representation of
object, or the empty String fornullobjects - Throws:
NullPointerException- ifinstantis null
-
instantFromTime64Integer
-
localTimeFromTime64Integer
-
durationToTimeString
Converts aDurationto a time string in the format[-]HH:mm:ss[.nnnnnnnnn].Unlike standard time formats, hours can exceed 24 and can be negative. The precision parameter controls the number of fractional second digits (0-9).
- Parameters:
duration- the duration to convertprecision- the number of fractional second digits (0-9)- Returns:
- a string representation like
-999:59:59.123456789 - Throws:
NullPointerException- ifdurationis null
-
localDateTimeToDuration
-
toLocalDate
Converts aDatetoLocalDateusing the specified timezone.For default JVM timezone behavior, use
Date.toLocalDate()directly.- Parameters:
sqlDate- the java.sql.Date to converttimeZone- the timezone context- Returns:
- the LocalDate representing the date in the specified timezone
- Throws:
NullPointerException- if sqlDate or timeZone is null
-
toLocalTime
Converts aTimetoLocalTimeusing the specified timezone.For default JVM timezone behavior, use
Time.toLocalTime()directly.- Parameters:
sqlTime- the java.sql.Time to converttimeZone- the timezone context- Returns:
- the LocalTime representing the time in the specified timezone
- Throws:
NullPointerException- if sqlTime or timeZone is null
-
toLocalDateTime
Converts aTimestamptoLocalDateTimeusing the specified timezone.Note: This method preserves nanosecond precision from the Timestamp.
For default JVM timezone behavior, use
Timestamp.toLocalDateTime()directly.- Parameters:
sqlTimestamp- the java.sql.Timestamp to converttimeZone- the timezone context- Returns:
- the LocalDateTime representing the timestamp in the specified timezone
- Throws:
NullPointerException- if sqlTimestamp or timeZone is null
-
toZonedDateTime
Converts aTimestamptoZonedDateTimeby expressing the timestamp's instant in the specified timezone.The underlying instant is preserved — only the timezone context changes. This matches the JDBC
setTimestamp(int, Timestamp, Calendar)contract where the Calendar's timezone is used to interpret the Timestamp's absolute point in time.Note: This method preserves nanosecond precision from the Timestamp.
- Parameters:
sqlTimestamp- the java.sql.Timestamp to converttimeZone- the timezone to express the instant in- Returns:
- the ZonedDateTime representing the same instant in the specified timezone
- Throws:
NullPointerException- if sqlTimestamp or timeZone is null
-
toSqlDate
Converts aLocalDatetoDateusing the specified timezone.For default JVM timezone behavior, use
Date.valueOf(LocalDate)directly.- Parameters:
localDate- the LocalDate to converttimeZone- the timezone context- Returns:
- the java.sql.Date representing midnight on the specified date in the given timezone
- Throws:
NullPointerException- if localDate or timeZone is null
-
toSqlTime
Converts aLocalTimetoTimeusing the specified timezone.For default JVM timezone behavior, use
Time.valueOf(LocalTime)directly.- Parameters:
localTime- the LocalTime to converttimeZone- the timezone context- Returns:
- the java.sql.Time representing the specified time
- Throws:
NullPointerException- if localTime or timeZone is null
-
toSqlTimestamp
Converts aLocalDateTimetoTimestampusing the specified timezone.Note: This method preserves nanosecond precision from the LocalDateTime.
For default JVM timezone behavior, use
Timestamp.valueOf(LocalDateTime)directly.- Parameters:
localDateTime- the LocalDateTime to converttimeZone- the timezone context- Returns:
- the java.sql.Timestamp representing the specified date and time
- Throws:
NullPointerException- if localDateTime or timeZone is null
-
toUnixTimestampString
-
toUnixTimestampString
Returns Unix Timestamp in nanoseconds as string- Parameters:
localTs- - LocalDateTime timestamplocalTz- - local timezone (useful to override default)- Returns:
- String value.
-
toUnixTimestampString
Returns Unix Timestamp in nanoseconds as string- Parameters:
instant- - instant to convert- Returns:
- String value.
-