org.postgresql.jdbc
Class TimestampUtils

java.lang.Object
  extended by org.postgresql.jdbc.TimestampUtils

public class TimestampUtils
extends Object

Misc utils for handling time and date values.


Method Summary
 Date convertToDate(long millis, TimeZone tz)
          Extracts the date part from a timestamp.
 Time convertToTime(long millis, TimeZone tz)
          Extracts the time part from a timestamp.
 String timeToString(Date time)
          Returns the given time value as String matching what the current postgresql server would send in text mode.
 void toBinDate(TimeZone tz, byte[] bytes, Date value)
          Converts the SQL Date to binary representation for Oid.DATE.
 Date toDate(Calendar cal, String s)
           
 Date toDateBin(TimeZone tz, byte[] bytes)
          Returns the SQL Date object matching the given bytes with Oid.DATE.
 String toString(Calendar cal, Date x)
           
 String toString(Calendar cal, Time x)
           
 String toString(Calendar cal, Timestamp x)
           
 Time toTime(Calendar cal, String s)
           
 Time toTimeBin(TimeZone tz, byte[] bytes)
          Returns the SQL Time object matching the given bytes with Oid.TIME or Oid.TIMETZ.
 Timestamp toTimestamp(Calendar cal, String s)
          Parse a string and return a timestamp representing its value.
 Timestamp toTimestampBin(TimeZone tz, byte[] bytes, boolean timestamptz)
          Returns the SQL Timestamp object matching the given bytes with Oid.TIMESTAMP or Oid.TIMESTAMPTZ.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 string
s - 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.

toTime

public Time toTime(Calendar cal,
                   String s)
            throws SQLException
Throws:
SQLException

toDate

public Date toDate(Calendar cal,
                   String s)
            throws SQLException
Throws:
SQLException

toString

public String toString(Calendar cal,
                       Timestamp x)

toString

public String toString(Calendar cal,
                       Date x)

toString

public String toString(Calendar cal,
                       Time x)

toDateBin

public Date toDateBin(TimeZone tz,
                      byte[] bytes)
               throws PSQLException
Returns the SQL Date object matching the given bytes with Oid.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.

toTimeBin

public Time toTimeBin(TimeZone tz,
                      byte[] bytes)
               throws PSQLException
Returns the SQL Time object matching the given bytes with Oid.TIME or Oid.TIMETZ.

Parameters:
tz - The timezone used when received data is Oid.TIME, ignored if data already contains Oid.TIMETZ.
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 with Oid.TIMESTAMP or Oid.TIMESTAMPTZ.

Parameters:
tz - The timezone used when received data is Oid.TIMESTAMP, ignored if data already contains Oid.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.

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)
Returns the given time value as String matching what the current postgresql server would send in text mode.

Parameters:
time - time value
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 for Oid.DATE.

Parameters:
tz - The timezone used.
bytes - The binary encoded date value.
value - value
Throws:
PSQLException - If binary format could not be parsed.


Copyright © 2016 PostgreSQL Global Development Group. All rights reserved.