Package org.postgresql.jdbc
Class TimestampUtils
- java.lang.Object
-
- org.postgresql.jdbc.TimestampUtils
-
public class TimestampUtils extends java.lang.ObjectMisc utils for handling time and date values.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.sql.DateconvertToDate(long millis, java.util.TimeZone tz)Extracts the date part from a timestamp.java.sql.TimeconvertToTime(long millis, java.util.TimeZone tz)Extracts the time part from a timestamp.java.util.CalendargetSharedCalendar(java.util.TimeZone timeZone)Get a shared calendar, applying the supplied time zone or the default time zone if null.booleanhasFastDefaultTimeZone()static java.util.TimeZoneparseBackendTimeZone(java.lang.String timeZone)Converts backend's TimeZone parameter to java format.java.lang.StringtimeToString(java.util.Date time, boolean withTimeZone)Returns the given time value as String matching what the current postgresql server would send in text mode.voidtoBinDate(java.util.TimeZone tz, byte[] bytes, java.sql.Date value)Converts the SQL Date to binary representation forOid.DATE.java.sql.DatetoDate(java.util.Calendar cal, java.lang.String s)java.sql.DatetoDateBin(java.util.TimeZone tz, byte[] bytes)Returns the SQL Date object matching the given bytes withOid.DATE.java.time.LocalDateTimetoLocalDateTime(java.lang.String s)Parse a string and return a LocalDateTime representing its value.java.time.LocalDateTimetoLocalDateTimeBin(byte[] bytes)Returns the local date time object matching the given bytes withOid.TIMESTAMPorOid.TIMESTAMPTZ.java.time.LocalTimetoLocalTime(java.lang.String s)Parse a string and return a LocalTime representing its value.java.time.LocalTimetoLocalTimeBin(byte[] bytes)Returns the SQL Time object matching the given bytes withOid.TIME.java.time.OffsetDateTimetoOffsetDateTime(java.lang.String s)Parse a string and return a LocalDateTime representing its value.java.time.OffsetDateTimetoOffsetDateTime(java.sql.Time t)Returns the offset date time object matching the given bytes with Oid#TIMETZ.java.time.OffsetDateTimetoOffsetDateTimeBin(byte[] bytes)Returns the offset date time object matching the given bytes with Oid#TIMESTAMPTZ.java.lang.StringtoString(java.time.LocalDate localDate)java.lang.StringtoString(java.time.LocalDateTime localDateTime)FormatsLocalDateTimeto be sent to the backend, thus it adds time zone.java.lang.StringtoString(java.time.LocalTime localTime)java.lang.StringtoString(java.time.OffsetDateTime offsetDateTime)java.lang.StringtoString(java.util.Calendar cal, java.sql.Date x)java.lang.StringtoString(java.util.Calendar cal, java.sql.Date x, boolean withTimeZone)java.lang.StringtoString(java.util.Calendar cal, java.sql.Time x)java.lang.StringtoString(java.util.Calendar cal, java.sql.Time x, boolean withTimeZone)java.lang.StringtoString(java.util.Calendar cal, java.sql.Timestamp x)java.lang.StringtoString(java.util.Calendar cal, java.sql.Timestamp x, boolean withTimeZone)java.sql.TimetoTime(java.util.Calendar cal, java.lang.String s)java.sql.TimetoTimeBin(java.util.TimeZone tz, byte[] bytes)Returns the SQL Time object matching the given bytes withOid.TIMEorOid.TIMETZ.java.sql.TimestamptoTimestamp(java.util.Calendar cal, java.lang.String s)Parse a string and return a timestamp representing its value.java.sql.TimestamptoTimestampBin(java.util.TimeZone tz, byte[] bytes, boolean timestamptz)Returns the SQL Timestamp object matching the given bytes withOid.TIMESTAMPorOid.TIMESTAMPTZ.
-
-
-
Method Detail
-
toTimestamp
public java.sql.Timestamp toTimestamp(java.util.Calendar cal, java.lang.String s) throws java.sql.SQLExceptionParse a string and return a timestamp representing its value.- Parameters:
cal- calendar to be used to parse the input strings- The ISO formated date string to parse.- Returns:
- null if s is null or a timestamp of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toLocalTime
public java.time.LocalTime toLocalTime(java.lang.String s) throws java.sql.SQLExceptionParse a string and return a LocalTime representing its value.- Parameters:
s- The ISO formated time string to parse.- Returns:
- null if s is null or a LocalTime of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toLocalDateTime
public java.time.LocalDateTime toLocalDateTime(java.lang.String s) throws java.sql.SQLExceptionParse a string and return a LocalDateTime representing its value.- Parameters:
s- The ISO formated date string to parse.- Returns:
- null if s is null or a LocalDateTime of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toOffsetDateTime
public java.time.OffsetDateTime toOffsetDateTime(java.lang.String s) throws java.sql.SQLExceptionParse a string and return a LocalDateTime representing its value.- Parameters:
s- The ISO formated date string to parse.- Returns:
- null if s is null or a LocalDateTime of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toOffsetDateTime
public java.time.OffsetDateTime toOffsetDateTime(java.sql.Time t)
Returns the offset date time object matching the given bytes with Oid#TIMETZ.- Parameters:
t- the time value- Returns:
- the matching offset date time
-
toOffsetDateTimeBin
public java.time.OffsetDateTime toOffsetDateTimeBin(byte[] bytes) throws PSQLExceptionReturns 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
public java.sql.Time toTime(java.util.Calendar cal, java.lang.String s) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
toDate
public java.sql.Date toDate(java.util.Calendar cal, java.lang.String s) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
getSharedCalendar
public java.util.Calendar getSharedCalendar(java.util.TimeZone timeZone)
Get a shared calendar, applying the supplied time zone or the default time zone if null.- Parameters:
timeZone- time zone to be set for the calendar- Returns:
- The shared calendar.
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Timestamp x)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Timestamp x, boolean withTimeZone)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Date x)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Date x, boolean withTimeZone)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Time x)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Time x, boolean withTimeZone)
-
toString
public java.lang.String toString(java.time.LocalDate localDate)
-
toString
public java.lang.String toString(java.time.LocalTime localTime)
-
toString
public java.lang.String toString(java.time.OffsetDateTime offsetDateTime)
-
toString
public java.lang.String toString(java.time.LocalDateTime localDateTime)
FormatsLocalDateTimeto 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
public java.sql.Date toDateBin(java.util.TimeZone tz, byte[] bytes) throws PSQLExceptionReturns 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
public java.sql.Time toTimeBin(java.util.TimeZone tz, byte[] bytes) throws PSQLExceptionReturns the SQL Time object matching the given bytes withOid.TIMEorOid.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
public java.time.LocalTime toLocalTimeBin(byte[] bytes) throws PSQLExceptionReturns 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 java.sql.Timestamp toTimestampBin(java.util.TimeZone tz, byte[] bytes, boolean timestamptz) throws PSQLExceptionReturns the SQL Timestamp object matching the given bytes withOid.TIMESTAMPorOid.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
public java.time.LocalDateTime toLocalDateTimeBin(byte[] bytes) throws PSQLExceptionReturns the local date time object matching the given bytes withOid.TIMESTAMPorOid.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.
-
convertToDate
public java.sql.Date convertToDate(long millis, java.util.TimeZone tz)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
public java.sql.Time convertToTime(long millis, java.util.TimeZone tz)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
public java.lang.String timeToString(java.util.Date time, boolean withTimeZone)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
public void toBinDate(java.util.TimeZone tz, byte[] bytes, java.sql.Date value) throws PSQLExceptionConverts 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
public static java.util.TimeZone parseBackendTimeZone(java.lang.String timeZone)
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
-
-