Class TimeUtil

java.lang.Object
com.mysql.cj.util.TimeUtil

public class TimeUtil
extends java.lang.Object
Time zone conversion routines and other time related methods
  • Field Details

  • Constructor Details

  • Method Details

    • nanoTimeAvailable

      public static boolean nanoTimeAvailable()
    • getCurrentTimeNanosOrMillis

      public static long getCurrentTimeNanosOrMillis()
    • getCanonicalTimeZone

      public static java.lang.String getCanonicalTimeZone​(java.lang.String timezoneStr, ExceptionInterceptor exceptionInterceptor)
      Returns the 'official' Java timezone name for the given timezone
      Parameters:
      timezoneStr - the 'common' timezone name
      exceptionInterceptor - exception interceptor
      Returns:
      the Java timezone name for the given timezone
    • adjustNanosPrecision

      public static java.sql.Timestamp adjustNanosPrecision​(java.sql.Timestamp ts, int fsp, boolean serverRoundFracSecs)
      Return a new Timestamp object which value is adjusted according to known DATE, DATETIME or TIMESTAMP field precision.
      Parameters:
      ts - an original Timestamp object, not modified by this method
      fsp - value in the range from 0 to 6 specifying fractional seconds precision
      serverRoundFracSecs - Flag indicating whether rounding or truncation occurs on server when inserting a TIME, DATE, or TIMESTAMP value with a fractional seconds part into a column having the same type but fewer fractional digits: true means rounding, false means truncation. The proper value should be detected by analyzing sql_mode server variable for TIME_TRUNCATE_FRACTIONAL presence.
      Returns:
      A new Timestamp object cloned from the original one and then rounded or truncated according to required fsp value
    • adjustNanosPrecision

      public static java.time.LocalDateTime adjustNanosPrecision​(java.time.LocalDateTime x, int fsp, boolean serverRoundFracSecs)
      Return a new LocalDateTime object which value is adjusted according to known DATE, DATETIME or TIMESTAMP field precision.
      Parameters:
      x - an original LocalDateTime object, not modified by this method
      fsp - value in the range from 0 to 6 specifying fractional seconds precision
      serverRoundFracSecs - Flag indicating whether rounding or truncation occurs on server when inserting a TIME, DATE, or TIMESTAMP value with a fractional seconds part into a column having the same type but fewer fractional digits: true means rounding, false means truncation. The proper value should be detected by analyzing sql_mode server variable for TIME_TRUNCATE_FRACTIONAL presence.
      Returns:
      A new LocalDateTime object cloned from the original one and then rounded or truncated according to required fsp value
    • adjustNanosPrecision

      public static java.time.LocalTime adjustNanosPrecision​(java.time.LocalTime x, int fsp, boolean serverRoundFracSecs)
    • formatNanos

      public static java.lang.String formatNanos​(int nanos, int fsp)
      Return a string representation of a fractional seconds part. This method assumes that all Timestamp adjustments are already done before, thus no rounding is needed, only a proper "0" padding to be done.
      Parameters:
      nanos - fractional seconds value
      fsp - required fractional part length
      Returns:
      fractional seconds part as a string
    • formatNanos

      public static java.lang.String formatNanos​(int nanos, int fsp, boolean truncateTrailingZeros)
      Return a string representation of a fractional seconds part. This method assumes that all Timestamp adjustments are already done before, thus no rounding is needed, only a proper "0" padding to be done.
      Parameters:
      nanos - fractional seconds value
      fsp - required fractional part length
      truncateTrailingZeros - whether to remove trailing zero characters in a fractional part after formatting
      Returns:
      fractional seconds part as a string
    • truncateFractionalSeconds

      public static java.sql.Timestamp truncateFractionalSeconds​(java.sql.Timestamp timestamp)
    • truncateFractionalSeconds

      public static java.sql.Time truncateFractionalSeconds​(java.sql.Time time)
    • hasFractionalSeconds

      public static java.lang.Boolean hasFractionalSeconds​(java.sql.Time t)
    • getSimpleDateFormat

      public static java.text.SimpleDateFormat getSimpleDateFormat​(java.text.SimpleDateFormat cachedSimpleDateFormat, java.lang.String pattern, java.util.TimeZone tz)
      Get SimpleDateFormat with a default Calendar which TimeZone is replaced with the provided one.

      Note: The SimpleDateFormat object returned by this method contains a default Calendar with an altered TimeZone. It's safe to cache it between this method calls because the Calendar object itself is not altered.

      Parameters:
      cachedSimpleDateFormat - existing SimpleDateFormat to use instead of creating a new one
      pattern - format pattern
      tz - TimeZone object replacing the default one
      Returns:
      SimpleDateFormat object
    • getSimpleDateFormat

      public static java.text.SimpleDateFormat getSimpleDateFormat​(java.lang.String pattern, java.util.Calendar cal)
      Get SimpleDateFormat where a default Calendar is replaced with the provided one.

      Note: Don't cache the SimpleDateFormat object returned by this method. Other methods could rely on assumption that the cached SimpleDateFormat has a default Calendar and that it is safe to change only it's time zone (see getSimpleDateFormat(SimpleDateFormat, String, TimeZone).

      Parameters:
      pattern - format pattern
      cal - Calendar object replacing the default one
      Returns:
      SimpleDateFormat object
    • getDateTimePattern

      public static final java.lang.String getDateTimePattern​(java.lang.String dt, boolean toTime) throws java.io.IOException
      Used in prepared statements
      Parameters:
      dt - DateTime string
      toTime - true if get Time pattern
      Returns:
      pattern
      Throws:
      java.io.IOException - if an error occurs