Top Description Inners Fields Constructors Methods
org.postgresql.jdbc

public Class TimestampUtils

extends Object
Class Inheritance
Static Imports
org.postgresql.util.internal.Nullness.castNonNull

Misc utils for handling time and date values.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum
private static class
private static class

Field Summary

Modifier and TypeField and Description
private @Nullable Calendar
private @Nullable ZoneOffset
private final Calendar
private static final @Nullable Field
private @Nullable TimeZone
private static final HashMap<String, TimeZone>
private final ResourceLock
private static final LocalDateTime
private static final int
private static final OffsetDateTime
private static final LocalTime
private static final LocalDate
private static final LocalDateTime
private static final OffsetDateTime
private static final char[][]
private static final Duration
private static final int
ONEDAY

Number of milliseconds in one day.

private static final Duration
private @Nullable TimeZone
private final StringBuilder
private final Provider<TimeZone>
private final boolean
usesDouble

True if the backend uses doubles for time values.

private static final TimeZone
private static final char[]

Constructor Summary

AccessConstructor and Description
public
TimestampUtils(boolean usesDouble, Provider<TimeZone> timeZoneProvider)

Method Summary

Modifier and TypeMethod and Description
private static void
private static void
appendDate(StringBuilder sb, int year, int month, int day)

private static void
private static void
private static void
private static void
appendTime(StringBuilder sb, Calendar cal, int nanos)

private static void
appendTime(StringBuilder
destination
sb
,
int
hours
hours
,
int
minutes
minutes
,
int
seconds
seconds
,
int
nanoseconds
nanos
)

Appends time part to the StringBuilder in PostgreSQL-compatible format.

private static void
private void
private void
appendTimeZone(StringBuilder sb, int offset)

private void
private static char
charAt(char[] s, int pos)

public Date

Returns:

The extracted date.
convertToDate
(long
The timestamp from which to extract the date.
millis
,
@Nullable TimeZone
The time zone of the date.
tz
)

Extracts the date part from a timestamp.

public Time

Returns:

The extracted time.
convertToTime
(long
The timestamp from which to extract the time.
millis
,
TimeZone
timezone to use.
tz
)

Extracts the time part from a timestamp.

private static int
firstNonDigit(char[] s, int start)

private static long
floorDiv(long x, long y)

private static long
floorMod(long x, long y)

private Calendar
private TimeZone
public Calendar

Returns:

The shared calendar.
getSharedCalendar
(@Nullable TimeZone
time zone to be set for the calendar
timeZone
)

Get a shared calendar, applying the supplied time zone or the default time zone if null.

private long

Returns:

timestamp that would be rendered in tz like millis in UTC
guessTimestamp
(long
source timestamp
millis
,
@Nullable TimeZone
desired time zone
tz
)

Given a UTC timestamp millis finds another point in time that is rendered in given time zone tz exactly as "millis in UTC".

public boolean
private static boolean
private static boolean

Returns:

true when microsecond part of the time should be increased when rounding to microseconds
nanosExceed499
(int
nanosecond part of the time
nanos
)

Returns true when microsecond part of the time should be increased when rounding to microseconds

private static int
number(char[] s, int start, int end)

private TimestampUtils.ParsedTimestamp
parseBackendTimestamp(String str)

Load date/time information into the provided calendar returning the fractional seconds.

public static TimeZone

Returns:

java TimeZone
parseBackendTimeZone
(String
time zone to use
timeZone
)

Converts backend's TimeZone parameter to java format.

private Calendar
private static int
skipWhitespace(char[] s, int start)

public String

Returns:

given time value as String
timeToString
(Date
time value
time
,
boolean
whether timezone should be added
withTimeZone
)

Returns the given time value as String matching what the current postgresql server would send in text mode.

public void
toBinDate(@Nullable TimeZone
The timezone used.
tz
,
byte[]
The binary encoded date value.
bytes
,
Date
value
value
)

Converts the SQL Date to binary representation for Oid#DATE.

public @PolyNull Date
public Date

Returns:

The parsed date object.
toDateBin
(@Nullable TimeZone
The timezone used.
tz
,
byte[]
The binary encoded date value.
bytes
)

Returns the SQL Date object matching the given bytes with Oid#DATE.

private static long

Returns:

Java seconds.
toJavaSecs
(long
Postgresql seconds.
secs
)

Converts the given postgresql seconds to java seconds.

public LocalDate

Returns:

The parsed local date object.
toLocalDateBin
(byte[]
The binary encoded local date value.
bytes
)

Returns the local date time object matching the given bytes with Oid#DATE or Oid#TIMESTAMP.

public @PolyNull LocalDateTime

Returns:

null if s is null or a LocalDateTime of the parsed string s.
toLocalDateTime
(@PolyNull String
The ISO formated date string to parse.
s
)

Parse a string and return a LocalDateTime representing its value.

public LocalDateTime

Returns:

The parsed local date time object.
toLocalDateTimeBin
(byte[]
The binary encoded local date time value.
bytes
)

Returns the local date time object matching the given bytes with Oid#TIMESTAMP or Oid#TIMESTAMPTZ.

public @PolyNull LocalTime

Returns:

null if s is null or a LocalTime of the parsed string s.
toLocalTime
(@PolyNull String
The ISO formated time string to parse.
s
)

Parse a string and return a LocalTime representing its value.

public LocalTime

Returns:

The parsed time object.
toLocalTimeBin
(byte[]
The binary encoded time value.
bytes
)

Returns the SQL Time object matching the given bytes with Oid#TIME.

public OffsetDateTime

Returns:

the matching offset date time
toOffsetDateTime
(Time
the time value
t
)
Deprecated was used internally, and not used anymore

Returns the offset date time object matching the given bytes with Oid#TIMETZ.

public @PolyNull OffsetDateTime

Returns:

null if s is null or a OffsetDateTime of the parsed string s.
toOffsetDateTime
(@PolyNull String
The ISO formatted date string to parse.
s
)

Parse a string and return a OffsetDateTime representing its value.

public OffsetDateTime

Returns:

The parsed local date time object.
toOffsetDateTimeBin
(byte[]
The binary encoded local date time value.
bytes
)

Returns the offset date time object matching the given bytes with Oid#TIMESTAMPTZ.

public @PolyNull OffsetTime

Returns:

null if s is null or a OffsetTime of the parsed string s.
toOffsetTime
(@PolyNull String
The ISO formated time string to parse.
s
)

Parse a string and return a OffsetTime representing its value.

public OffsetTime

Returns:

The parsed offset time object.
toOffsetTimeBin
(byte[]
The binary encoded TIMETZ/TIME value.
bytes
)

Returns the offset time object matching the given bytes with Oid#TIMETZ or Oid#TIME.

private TimestampUtils.ParsedBinaryTimestamp
toParsedTimestampBin(@Nullable TimeZone tz, byte[] bytes, boolean timestamptz)

private TimestampUtils.ParsedBinaryTimestamp
private static long

Returns:

Java seconds.
toPgSecs
(long
Postgresql seconds.
secs
)

Converts the given java seconds to postgresql seconds.

private TimestampUtils.ParsedBinaryTimestamp
public String
public String
toString(@Nullable Calendar cal, Timestamp x, boolean withTimeZone)

public String
public String
toString(@Nullable Calendar cal, Date x, boolean withTimeZone)

public String
public String
toString(@Nullable Calendar cal, Time x, boolean withTimeZone)

public String
toString(LocalDate localDate)

public String
toString(LocalTime localTime)

public String
toString(OffsetTime offsetTime)

public String
toString(OffsetDateTime offsetDateTime)

public String

Returns:

The formatted local date
toString
(LocalDateTime
The local date to format as a String
localDateTime
)

Formats LocalDateTime to be sent to the backend, thus it adds time zone.

public String

Returns:

string representation of timestamptz
toStringOffsetDateTime
(byte[]
binary representation of timestamptz
value
)

Converts timestamptz to string taking client time zone (timeZoneProvider) into account.

public String

Returns:

string representation of timetz
toStringOffsetTimeBin
(byte[]
binary representation of timetz
value
)

Converts timetz to string taking client time zone (timeZoneProvider) into account.

public @PolyNull Time
public Time

Returns:

The parsed time object.
toTimeBin
(@Nullable TimeZone
The timezone used when received data is Oid#TIME, ignored if data already contains Oid#TIMETZ.
tz
,
byte[]
The binary encoded time value.
bytes
)

Returns the SQL Time object matching the given bytes with Oid#TIME or Oid#TIMETZ.

public @PolyNull Timestamp

Returns:

null if s is null or a timestamp of the parsed string s.
toTimestamp
(@Nullable Calendar
calendar to be used to parse the input string
cal
,
@PolyNull String
The ISO formated date string to parse.
s
)

Parse a string and return a timestamp representing its value.

public Timestamp

Returns:

The parsed timestamp object.
toTimestampBin
(@Nullable TimeZone
The timezone used when received data is Oid#TIMESTAMP, ignored if data already contains Oid#TIMESTAMPTZ.
tz
,
byte[]
The binary encoded timestamp value.
bytes
,
boolean
True if the binary is in GMT.
timestamptz
)

Returns the SQL Timestamp object matching the given bytes with Oid#TIMESTAMP or Oid#TIMESTAMPTZ.

public OffsetTime

Returns:

adjusted offset time (it represents the same instant as the input one)
withClientOffsetSameInstant
(OffsetTime
input offset time
input
)

PostgreSQL does not store the time zone in the binary representation of timetz.

public OffsetDateTime

Returns:

adjusted offset date time (it represents the same instant as the input one)
withClientOffsetSameInstant
(OffsetDateTime
input offset date time
input
)

PostgreSQL does not store the time zone in the binary representation of timestamptz.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait