14.2.2.5 Accessing Datetimes from PGQL Result Sets

You can use the following APIs for retrieving datetime values from PGQL result sets.

LocalDate getDate(int elementIdx)
LocalDate getDate(String variableName)
LocalTime getTime(int elementIdx)
LocalTime getTime(String variableName)
LocalDateTime getTimestamp(int elementIdx)
LocalDateTime getTimestamp(String variableName)
OffsetTime getTimeWithTimezone(int elementIdx)
OffsetTime getTimeWithTimezone(String variableName)
OffsetDateTime getTimestampWithTimezone(int elementIdx)
OffsetDateTime getTimestampWithTimezone(String variableName)

The following example prints the birthdays of all the persons in the graph is as follows:

opg4j> var resultSet = session.queryPgql("""
  SELECT n.name, n.date_of_birth
    FROM MATCH (n) ON people_graph
ORDER BY n.name
""")
opg4j> while (resultSet.next()) {
...>  System.out.println(resultSet.getString(1) + " has birthday " + resultSet.getDate(2));
...> }
opg4j> resultSet.close()
import java.time.LocalDate;
import oracle.pgx.api.*;
...
PgqlResultSet resultSet = session.queryPgql(
  "  SELECT n.name, n.date_of_birth\n" +
  "    FROM MATCH (n) ON people_graph\n" +
  "ORDER BY n.name");

while (resultSet.next()) {
  System.out.println(resultSet.getString(1) + " has birthday " + resultSet.getDate(2));
}

resultSet.close();

The result of the query is as follows:

Judy has birthday 1989-01-15
Klara has birthday 2001-01-29
Pete has birthday 1995-08-01

In addition to the Java types from the new java.time package, the legacy java.util.Date is also supported through the following APIs:

Date getLegacyDate(int elementIdx)
Date getLegacyDate(String variableName)

Note:

The legacy java.util.Date can store dates, times, as well as timestamps, so these two APIs can be used for accessing values of any of the five datetime types.