Class DataTypeUtils

java.lang.Object
com.clickhouse.client.api.DataTypeUtils

public class DataTypeUtils extends Object
  • Field Details

    • DATETIME_FORMATTER

      public static DateTimeFormatter DATETIME_FORMATTER
      Formatter for the DateTime type.
    • DATE_FORMATTER

      public static DateTimeFormatter DATE_FORMATTER
      Formatter for the Date type.
    • DATETIME_WITH_NANOS_FORMATTER

      public static DateTimeFormatter DATETIME_WITH_NANOS_FORMATTER
      Formatter for the DateTime type with nanoseconds.
    • TIME_WITH_NANOS_FORMATTER

      public static final DateTimeFormatter TIME_WITH_NANOS_FORMATTER
    • TIME_FORMATTER

      public static final DateTimeFormatter TIME_FORMATTER
    • DATE_TIME_WITH_OPTIONAL_NANOS

      public static final DateTimeFormatter DATE_TIME_WITH_OPTIONAL_NANOS
  • Constructor Details

    • DataTypeUtils

      public DataTypeUtils()
  • Method Details

    • formatInstant

      public static String formatInstant(Instant instant)
      Formats an Instant object 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 - if instant is null
    • formatInstant

      public static String formatInstant(Instant instant, ClickHouseDataType dataTypeHint)
      Formats an Instant object for use in SQL statements or as query parameter. This method uses the dataTypeHint parameter to find the best suitable format for the instant.
      Parameters:
      instant - the Java object to format
      dataTypeHint - the ClickHouse data type instant should be used for
      Returns:
      a suitable String representation of instant
      Throws:
      NullPointerException - if instant is null
    • formatInstant

      public static String formatInstant(Instant instant, ClickHouseDataType dataTypeHint, ZoneId timeZone)
      Formats an Instant object for use in SQL statements or as query parameter. This method uses the dataTypeHint parameter to find the best suitable format for the instant. For some formatting operations, providing a timeZone is mandatory, e.g. for ClickHouseDataType.Date.
      Parameters:
      instant - the Java object to format
      dataTypeHint - the ClickHouse data type object should be used for
      timeZone - 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 for null objects
      Throws:
      NullPointerException - if instant is null
    • instantFromTime64Integer

      public static Instant instantFromTime64Integer(int precision, long value)
    • localTimeFromTime64Integer

      public static LocalDateTime localTimeFromTime64Integer(int precision, long value)
    • durationToTimeString

      public static String durationToTimeString(Duration duration, int precision)
      Converts a Duration to 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 convert
      precision - the number of fractional second digits (0-9)
      Returns:
      a string representation like -999:59:59.123456789
      Throws:
      NullPointerException - if duration is null
    • localDateTimeToDuration

      public static Duration localDateTimeToDuration(LocalDateTime localDateTime)
    • toLocalDate

      public static LocalDate toLocalDate(Date sqlDate, TimeZone timeZone)
      Converts a Date to LocalDate using the specified timezone.

      For default JVM timezone behavior, use Date.toLocalDate() directly.

      Parameters:
      sqlDate - the java.sql.Date to convert
      timeZone - the timezone context
      Returns:
      the LocalDate representing the date in the specified timezone
      Throws:
      NullPointerException - if sqlDate or timeZone is null
    • toLocalTime

      public static LocalTime toLocalTime(Time sqlTime, TimeZone timeZone)
      Converts a Time to LocalTime using the specified timezone.

      For default JVM timezone behavior, use Time.toLocalTime() directly.

      Parameters:
      sqlTime - the java.sql.Time to convert
      timeZone - the timezone context
      Returns:
      the LocalTime representing the time in the specified timezone
      Throws:
      NullPointerException - if sqlTime or timeZone is null
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Timestamp sqlTimestamp, TimeZone timeZone)
      Converts a Timestamp to LocalDateTime using 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 convert
      timeZone - the timezone context
      Returns:
      the LocalDateTime representing the timestamp in the specified timezone
      Throws:
      NullPointerException - if sqlTimestamp or timeZone is null
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(Timestamp sqlTimestamp, TimeZone timeZone)
      Converts a Timestamp to ZonedDateTime by 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 convert
      timeZone - 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

      public static Date toSqlDate(LocalDate localDate, TimeZone timeZone)
      Converts a LocalDate to Date using the specified timezone.

      For default JVM timezone behavior, use Date.valueOf(LocalDate) directly.

      Parameters:
      localDate - the LocalDate to convert
      timeZone - 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

      public static Time toSqlTime(LocalTime localTime, TimeZone timeZone)
      Converts a LocalTime to Time using the specified timezone.

      For default JVM timezone behavior, use Time.valueOf(LocalTime) directly.

      Parameters:
      localTime - the LocalTime to convert
      timeZone - the timezone context
      Returns:
      the java.sql.Time representing the specified time
      Throws:
      NullPointerException - if localTime or timeZone is null
    • toSqlTimestamp

      public static Timestamp toSqlTimestamp(LocalDateTime localDateTime, TimeZone timeZone)
      Converts a LocalDateTime to Timestamp using 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 convert
      timeZone - the timezone context
      Returns:
      the java.sql.Timestamp representing the specified date and time
      Throws:
      NullPointerException - if localDateTime or timeZone is null
    • toUnixTimestampString

      public static String toUnixTimestampString(long seconds, int nanos)
    • toUnixTimestampString

      public static String toUnixTimestampString(LocalDateTime localTs, TimeZone localTz)
      Returns Unix Timestamp in nanoseconds as string
      Parameters:
      localTs - - LocalDateTime timestamp
      localTz - - local timezone (useful to override default)
      Returns:
      String value.
    • toUnixTimestampString

      public static String toUnixTimestampString(Instant instant)
      Returns Unix Timestamp in nanoseconds as string
      Parameters:
      instant - - instant to convert
      Returns:
      String value.