Package com.mysql.cj.util
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 Summary
Fields Modifier and Type Field Description static java.time.format.DateTimeFormatterDATE_FORMATTERstatic java.time.format.DateTimeFormatterDATETIME_FORMATTER_NO_FRACT_NO_OFFSETstatic java.time.format.DateTimeFormatterDATETIME_FORMATTER_NO_FRACT_WITH_OFFSETstatic java.time.format.DateTimeFormatterDATETIME_FORMATTER_WITH_MILLIS_NO_OFFSETstatic java.time.format.DateTimeFormatterDATETIME_FORMATTER_WITH_NANOS_NO_OFFSETstatic java.time.format.DateTimeFormatterDATETIME_FORMATTER_WITH_NANOS_WITH_OFFSETprotected static java.lang.reflect.MethodsystemNanoTimeMethodstatic java.time.format.DateTimeFormatterTIME_FORMATTER_NO_FRACT_NO_OFFSETstatic java.time.format.DateTimeFormatterTIME_FORMATTER_NO_FRACT_WITH_OFFSETstatic java.time.format.DateTimeFormatterTIME_FORMATTER_WITH_NANOS_NO_OFFSETstatic java.time.format.DateTimeFormatterTIME_FORMATTER_WITH_NANOS_WITH_OFFSET -
Constructor Summary
Constructors Constructor Description TimeUtil() -
Method Summary
Modifier and Type Method Description static java.sql.TimestampadjustNanosPrecision(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.static java.time.LocalDateTimeadjustNanosPrecision(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.static java.time.LocalTimeadjustNanosPrecision(java.time.LocalTime x, int fsp, boolean serverRoundFracSecs)static java.lang.StringformatNanos(int nanos, int fsp)Return a string representation of a fractional seconds part.static java.lang.StringformatNanos(int nanos, int fsp, boolean truncateTrailingZeros)Return a string representation of a fractional seconds part.static java.lang.StringgetCanonicalTimeZone(java.lang.String timezoneStr, ExceptionInterceptor exceptionInterceptor)Returns the 'official' Java timezone name for the given timezonestatic longgetCurrentTimeNanosOrMillis()static java.lang.StringgetDateTimePattern(java.lang.String dt, boolean toTime)Used in prepared statementsstatic java.text.SimpleDateFormatgetSimpleDateFormat(java.lang.String pattern, java.util.Calendar cal)Get SimpleDateFormat where a default Calendar is replaced with the provided one.static java.text.SimpleDateFormatgetSimpleDateFormat(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.static java.lang.BooleanhasFractionalSeconds(java.sql.Time t)static booleannanoTimeAvailable()static java.sql.TimetruncateFractionalSeconds(java.sql.Time time)static java.sql.TimestamptruncateFractionalSeconds(java.sql.Timestamp timestamp)
-
Field Details
-
DATE_FORMATTER
public static final java.time.format.DateTimeFormatter DATE_FORMATTER -
TIME_FORMATTER_NO_FRACT_NO_OFFSET
public static final java.time.format.DateTimeFormatter TIME_FORMATTER_NO_FRACT_NO_OFFSET -
TIME_FORMATTER_WITH_NANOS_NO_OFFSET
public static final java.time.format.DateTimeFormatter TIME_FORMATTER_WITH_NANOS_NO_OFFSET -
TIME_FORMATTER_NO_FRACT_WITH_OFFSET
public static final java.time.format.DateTimeFormatter TIME_FORMATTER_NO_FRACT_WITH_OFFSET -
TIME_FORMATTER_WITH_NANOS_WITH_OFFSET
public static final java.time.format.DateTimeFormatter TIME_FORMATTER_WITH_NANOS_WITH_OFFSET -
DATETIME_FORMATTER_NO_FRACT_NO_OFFSET
public static final java.time.format.DateTimeFormatter DATETIME_FORMATTER_NO_FRACT_NO_OFFSET -
DATETIME_FORMATTER_WITH_MILLIS_NO_OFFSET
public static final java.time.format.DateTimeFormatter DATETIME_FORMATTER_WITH_MILLIS_NO_OFFSET -
DATETIME_FORMATTER_WITH_NANOS_NO_OFFSET
public static final java.time.format.DateTimeFormatter DATETIME_FORMATTER_WITH_NANOS_NO_OFFSET -
DATETIME_FORMATTER_NO_FRACT_WITH_OFFSET
public static final java.time.format.DateTimeFormatter DATETIME_FORMATTER_NO_FRACT_WITH_OFFSET -
DATETIME_FORMATTER_WITH_NANOS_WITH_OFFSET
public static final java.time.format.DateTimeFormatter DATETIME_FORMATTER_WITH_NANOS_WITH_OFFSET -
systemNanoTimeMethod
protected static final java.lang.reflect.Method systemNanoTimeMethod
-
-
Constructor Details
-
TimeUtil
public TimeUtil()
-
-
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 nameexceptionInterceptor- 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 methodfsp- value in the range from 0 to 6 specifying fractional seconds precisionserverRoundFracSecs- 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 methodfsp- value in the range from 0 to 6 specifying fractional seconds precisionserverRoundFracSecs- 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 valuefsp- 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 valuefsp- required fractional part lengthtruncateTrailingZeros- 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 onepattern- format patterntz-TimeZoneobject replacing the default one- Returns:
SimpleDateFormatobject
-
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 patterncal-Calendarobject replacing the default one- Returns:
SimpleDateFormatobject
-
getDateTimePattern
public static final java.lang.String getDateTimePattern(java.lang.String dt, boolean toTime) throws java.io.IOExceptionUsed in prepared statements- Parameters:
dt- DateTime stringtoTime- true if get Time pattern- Returns:
- pattern
- Throws:
java.io.IOException- if an error occurs
-