Class TimeUtil

java.lang.Object
com.mysql.jdbc.TimeUtil

public class TimeUtil extends Object
Timezone conversion routines and other time related methods
  • Field Details

    • GMT_TIMEZONE

      static final TimeZone GMT_TIMEZONE
    • systemNanoTimeMethod

      protected static final Method systemNanoTimeMethod
  • Constructor Details

    • TimeUtil

      public TimeUtil()
  • Method Details

    • nanoTimeAvailable

      public static boolean nanoTimeAvailable()
    • getDefaultTimeZone

      public static final TimeZone getDefaultTimeZone(boolean useCache)
    • getCurrentTimeNanosOrMillis

      public static long getCurrentTimeNanosOrMillis()
    • changeTimezone

      public static Time changeTimezone(MySQLConnection conn, Calendar sessionCalendar, Calendar targetCalendar, Time t, TimeZone fromTz, TimeZone toTz, boolean rollForward)
      Change the given times from one timezone to another
      Parameters:
      conn - the current connection to the MySQL server
      t - the times to change
      fromTz - the timezone to change from
      toTz - the timezone to change to
      Returns:
      the times changed to the timezone 'toTz'
    • changeTimezone

      public static Timestamp changeTimezone(MySQLConnection conn, Calendar sessionCalendar, Calendar targetCalendar, Timestamp tstamp, TimeZone fromTz, TimeZone toTz, boolean rollForward)
      Change the given timestamp from one timezone to another
      Parameters:
      conn - the current connection to the MySQL server
      tstamp - the timestamp to change
      fromTz - the timezone to change from
      toTz - the timezone to change to
      Returns:
      the timestamp changed to the timezone 'toTz'
    • fastDateCreate

      static final Date fastDateCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day)
    • fastDateCreate

      static final Date fastDateCreate(int year, int month, int day, Calendar targetCalendar)
    • fastTimeCreate

      static final Time fastTimeCreate(Calendar cal, int hour, int minute, int second, ExceptionInterceptor exceptionInterceptor) throws SQLException
      Throws:
      SQLException
    • fastTimeCreate

      static final Time fastTimeCreate(int hour, int minute, int second, Calendar targetCalendar, ExceptionInterceptor exceptionInterceptor) throws SQLException
      Throws:
      SQLException
    • fastTimestampCreate

      static final Timestamp fastTimestampCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day, int hour, int minute, int seconds, int secondsPart)
    • fastTimestampCreate

      static final Timestamp fastTimestampCreate(TimeZone tz, int year, int month, int day, int hour, int minute, int seconds, int secondsPart)
    • getCanonicalTimezone

      public static String getCanonicalTimezone(String timezoneStr, ExceptionInterceptor exceptionInterceptor) throws SQLException
      Returns the 'official' Java timezone name for the given timezone
      Parameters:
      timezoneStr - the 'common' timezone name
      Returns:
      the Java timezone name for the given timezone
      Throws:
      SQLException
      IllegalArgumentException
    • adjustTimestampNanosPrecision

      public static Timestamp adjustTimestampNanosPrecision(Timestamp ts, int fsp, boolean serverRoundFracSecs) throws SQLException
      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 original ones and then rounded or truncated according to required fsp value
      Throws:
      SQLException - if fsp value is out of range
    • formatNanos

      public static String formatNanos(int nanos, boolean serverSupportsFracSecs, int fsp) throws SQLException
      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
      serverSupportsFracSecs - flag indicating does server support fractional seconds
      fsp - required fractional part length
      Returns:
      fractional seconds part as a string
      Throws:
      SQLException - if nanos or fsp value is out of range
    • truncateFractionalSeconds

      public static Timestamp truncateFractionalSeconds(Timestamp timestamp)
    • getSimpleDateFormat

      public static SimpleDateFormat getSimpleDateFormat(SimpleDateFormat cachedSimpleDateFormat, String pattern, Calendar cal, TimeZone tz)
    • setProlepticIfNeeded

      public static Calendar setProlepticIfNeeded(Calendar origCalendar, Calendar refCalendar)
      Return the proleptic version of origCalendar if refCalendar is proleptic. Applied only to GregorianCalendar parameters.
      Parameters:
      origCalendar - original Calendar
      refCalendar - reference Calendar
      Returns:
      the original Calendar if no adjustments are needed or the new proleptic GregorianCalendar with preserved Timezone of origCalendar and other fields unset.