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