Package org.postgresql.jdbc
Class TimestampUtils
java.lang.Object
org.postgresql.jdbc.TimestampUtils
Misc utils for handling time and date values.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionconvertToDate
(long millis, TimeZone tz) Extracts the date part from a timestamp.convertToTime
(long millis, TimeZone tz) Extracts the time part from a timestamp.getSharedCalendar
(TimeZone timeZone) Get a shared calendar, applying the supplied time zone or the default time zone if null.boolean
static TimeZone
parseBackendTimeZone
(String timeZone) Converts backend's TimeZone parameter to java format.timeToString
(Date time, boolean withTimeZone) Returns the given time value as String matching what the current postgresql server would send in text mode.void
Converts the SQL Date to binary representation forOid.DATE
.Deprecated.Returns the SQL Date object matching the given bytes withOid.DATE
.toLocalDate
(byte[] dateBytes) toLocalDateBin
(byte[] bytes) Returns the local date time object matching the given bytes withOid.DATE
orOid.TIMESTAMP
.toLocalDateTime
(byte[] bytes) Parse an array of bytes and return a LocalDateTime representing its value.Deprecated.toLocalDateTimeBin
(byte[] bytes) Returns the local date time object matching the given bytes withOid.TIMESTAMP
orOid.TIMESTAMPTZ
.Parse a string and return a LocalTime representing its value.toLocalTimeBin
(byte[] bytes) Returns the SQL Time object matching the given bytes withOid.TIME
.toOffsetDateTime
(byte[] bytes) Parse an array of bytes and return a OffsetDateTime representing its value.Deprecated.Deprecated.was used internally, and not used anymoretoOffsetDateTimeBin
(byte[] bytes) Returns the offset date time object matching the given bytes with Oid#TIMESTAMPTZ.toOffsetTime
(byte[] bytes) Parse an array of bytes and return a OffsetTime representing its value.Deprecated.in usetoOffsetTime(byte[])
insteadtoOffsetTimeBin
(byte[] bytes) Returns the offset time object matching the given bytes with Oid#TIMETZ or Oid#TIME.toString
(LocalDateTime localDateTime) FormatsLocalDateTime
to be sent to the backend, thus it adds time zone.toString
(OffsetDateTime offsetDateTime) toString
(OffsetTime offsetTime) toStringOffsetDateTime
(byte[] value) Convertstimestamptz
to string taking client time zone (timeZoneProvider
) into account.toStringOffsetTimeBin
(byte[] value) Convertstimetz
to string taking client time zone (timeZoneProvider
) into account.Deprecated.Returns the SQL Time object matching the given bytes withOid.TIME
orOid.TIMETZ
.toTimestamp
(Calendar cal, byte[] bytes) Parse an array of bytes and return a timestamp representing its value.toTimestamp
(Calendar cal, String s) Deprecated.toTimestampBin
(TimeZone tz, byte[] bytes, boolean timestamptz) Returns the SQL Timestamp object matching the given bytes withOid.TIMESTAMP
orOid.TIMESTAMPTZ
.PostgreSQL does not store the time zone in the binary representation of timestamptz.PostgreSQL does not store the time zone in the binary representation of timetz.
-
Constructor Details
-
TimestampUtils
-
-
Method Details
-
toTimestamp
Deprecated.Parse a string and return a timestamp representing its value.- Parameters:
cal
- calendar to be used to parse the input strings
- The ISO formatted date string to parse.- Returns:
- null if s is null or a timestamp of the parsed string s.
- Throws:
SQLException
- if there is a problem parsing s.
-
toTimestamp
Parse an array of bytes and return a timestamp representing its value.- Parameters:
cal
- calendar to be used to parse the input bytesbytes
- The ISO formatted date to parse.- Returns:
- null if bytes is null or a timestamp of the parsed bytes.
- Throws:
SQLException
- if there is a problem parsing bytes.
-
toLocalTime
Parse a string and return a LocalTime representing its value.- Parameters:
s
- The ISO formatted time string to parse.- Returns:
- null if s is null or a LocalTime of the parsed string s.
- Throws:
SQLException
- if there is a problem parsing s.
-
toOffsetTimeBin
Returns the offset time object matching the given bytes with Oid#TIMETZ or Oid#TIME.- Parameters:
bytes
- The binary encoded TIMETZ/TIME value.- Returns:
- The parsed offset time object.
- Throws:
PSQLException
- If binary format could not be parsed.
-
toOffsetTime
Deprecated.in usetoOffsetTime(byte[])
insteadParse a string and return a OffsetTime representing its value.- Parameters:
s
- The ISO formatted time string to parse.- Returns:
- null if s is null or a OffsetTime of the parsed string s.
- Throws:
SQLException
- if there is a problem parsing s.
-
toOffsetTime
Parse an array of bytes and return a OffsetTime representing its value.- Parameters:
bytes
- The ISO time formatted array of bytes time to parse.- Returns:
- null if bytes are null or a OffsetTime of the parsed string .
- Throws:
SQLException
- if there is a problem parsing bytes.
-
toLocalDateTime
Deprecated.- Parameters:
s
- The ISO formatted date string to parse.- Returns:
- null if s is null or a LocalDateTime of the parsed string s.
- Throws:
SQLException
- if there is a problem parsing s.
-
toLocalDateTime
Parse an array of bytes and return a LocalDateTime representing its value.- Parameters:
bytes
- The ISO formatted date array of bytes to parse.- Returns:
- null if s is null or a LocalDateTime of the parsed string s.
- Throws:
SQLException
- if there is a problem parsing bytes.
-
toOffsetDateTime
Deprecated.was used internally, and not used anymoreReturns the offset date time object matching the given bytes with Oid#TIMETZ. Not used internally anymore, function is here to retain compatibility with previous versions- Parameters:
t
- the time value- Returns:
- the matching offset date time
-
toOffsetDateTime
Deprecated.Parse a string and return a OffsetDateTime representing its value.- Parameters:
s
- The ISO formatted date string to parse.- Returns:
- null if s is null or a OffsetDateTime of the parsed string s.
- Throws:
SQLException
- if there is a problem parsing s.
-
toOffsetDateTime
Parse an array of bytes and return a OffsetDateTime representing its value.- Parameters:
bytes
- The ISO formatted date string to parse.- Returns:
- null if bytes is null or an OffsetDateTime of the parsed array of bytes.
- Throws:
SQLException
- if there is a problem parsing bytes.
-
toOffsetDateTimeBin
Returns the offset date time object matching the given bytes with Oid#TIMESTAMPTZ.- Parameters:
bytes
- The binary encoded local date time value.- Returns:
- The parsed local date time object.
- Throws:
PSQLException
- If binary format could not be parsed.
-
toTime
Deprecated.- Throws:
SQLException
-
toTime
- Throws:
SQLException
-
toDate
Deprecated.- Throws:
SQLException
-
toDate
- Throws:
SQLException
-
toLocalDate
- Throws:
SQLException
-
toString
-
toString
-
toString
-
toString
-
toString
-
toString
-
toString
-
toString
-
toString
-
toStringOffsetTimeBin
Convertstimetz
to string taking client time zone (timeZoneProvider
) into account.- Parameters:
value
- binary representation oftimetz
- Returns:
- string representation of
timetz
- Throws:
PSQLException
-
withClientOffsetSameInstant
PostgreSQL does not store the time zone in the binary representation of timetz. However, we want to preserve the output ofgetString()
in both binary and text formats So we try a client time zone when serializingOffsetTime
to string.- Parameters:
input
- input offset time- Returns:
- adjusted offset time (it represents the same instant as the input one)
-
toString
-
toStringOffsetDateTime
Convertstimestamptz
to string taking client time zone (timeZoneProvider
) into account.- Parameters:
value
- binary representation oftimestamptz
- Returns:
- string representation of
timestamptz
- Throws:
PSQLException
-
withClientOffsetSameInstant
PostgreSQL does not store the time zone in the binary representation of timestamptz. However, we want to preserve the output ofgetString()
in both binary and text formats So we try a client time zone when serializingOffsetDateTime
to string.- Parameters:
input
- input offset date time- Returns:
- adjusted offset date time (it represents the same instant as the input one)
-
toString
FormatsLocalDateTime
to be sent to the backend, thus it adds time zone. Do not use this method inResultSet.getString(int)
- Parameters:
localDateTime
- The local date to format as a String- Returns:
- The formatted local date
-
toDateBin
Returns the SQL Date object matching the given bytes withOid.DATE
.- Parameters:
tz
- The timezone used.bytes
- The binary encoded date value.- Returns:
- The parsed date object.
- Throws:
PSQLException
- If binary format could not be parsed.
-
hasFastDefaultTimeZone
public boolean hasFastDefaultTimeZone() -
toTimeBin
Returns the SQL Time object matching the given bytes withOid.TIME
orOid.TIMETZ
.- Parameters:
tz
- The timezone used when received data isOid.TIME
, ignored if data already containsOid.TIMETZ
.bytes
- The binary encoded time value.- Returns:
- The parsed time object.
- Throws:
PSQLException
- If binary format could not be parsed.
-
toLocalTimeBin
Returns the SQL Time object matching the given bytes withOid.TIME
.- Parameters:
bytes
- The binary encoded time value.- Returns:
- The parsed time object.
- Throws:
PSQLException
- If binary format could not be parsed.
-
toTimestampBin
public Timestamp toTimestampBin(TimeZone tz, byte[] bytes, boolean timestamptz) throws PSQLException Returns the SQL Timestamp object matching the given bytes withOid.TIMESTAMP
orOid.TIMESTAMPTZ
.- Parameters:
tz
- The timezone used when received data isOid.TIMESTAMP
, ignored if data already containsOid.TIMESTAMPTZ
.bytes
- The binary encoded timestamp value.timestamptz
- True if the binary is in GMT.- Returns:
- The parsed timestamp object.
- Throws:
PSQLException
- If binary format could not be parsed.
-
toLocalDateTimeBin
Returns the local date time object matching the given bytes withOid.TIMESTAMP
orOid.TIMESTAMPTZ
.- Parameters:
bytes
- The binary encoded local date time value.- Returns:
- The parsed local date time object.
- Throws:
PSQLException
- If binary format could not be parsed.
-
toLocalDateBin
Returns the local date time object matching the given bytes withOid.DATE
orOid.TIMESTAMP
.- Parameters:
bytes
- The binary encoded local date value.- Returns:
- The parsed local date object.
- Throws:
PSQLException
- If binary format could not be parsed.
-
convertToDate
Extracts the date part from a timestamp.- Parameters:
millis
- The timestamp from which to extract the date.tz
- The time zone of the date.- Returns:
- The extracted date.
-
convertToTime
Extracts the time part from a timestamp. This method ensures the date part of output timestamp looks like 1970-01-01 in given timezone.- Parameters:
millis
- The timestamp from which to extract the time.tz
- timezone to use.- Returns:
- The extracted time.
-
timeToString
Returns the given time value as String matching what the current postgresql server would send in text mode.- Parameters:
time
- time valuewithTimeZone
- whether timezone should be added- Returns:
- given time value as String
-
toBinDate
Converts the SQL Date to binary representation forOid.DATE
.- Parameters:
tz
- The timezone used.bytes
- The binary encoded date value.value
- value- Throws:
PSQLException
- If binary format could not be parsed.
-
parseBackendTimeZone
Converts backend's TimeZone parameter to java format. Notable difference: backend's gmt-3 is GMT+03 in Java.- Parameters:
timeZone
- time zone to use- Returns:
- java TimeZone
-
toLocalDateTime(byte[])