Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a Postgres timestamp field using java?

My data is in format:

2010-12-01 09:59:00.423

getDate in Java only returns the date portion. Is there a way to also extract the time?

like image 416
deltanovember Avatar asked May 31 '11 02:05

deltanovember


People also ask

How does java store dates in PostgreSQL?

You can use the LocalDate, LocalTime, LocalDateTime or OffsetDateTime (OffsetDateTime is an immutable representation of a date-time with an offset eg:the value "20th July 2017 at 10:45.24. 123456789 +04:00" can be stored in an OffsetDateTime). These classes comes in the java.

How are timestamps stored in Postgres?

PostgreSQL stores the timestamptz in UTC value. When you insert a value into a timestamptz column, PostgreSQL converts the timestamptz value into a UTC value and stores the UTC value in the table.

What is Postgres timestamp format?

Postgres DATE data type Postgres uses the DATE data type for storing different dates in YYYY-MM-DD format. It uses 4 bytes for storing a date value in a column. You can design a Postgres table with a DATE column and use the keyword DEFAULT CURRENT_DATE to use the current system date as the default value in this column.


1 Answers

The SQL DATE type indeed only contains the date portion, not the time. But your column is apparently of TIMESTAMP type, so to get the full timestamp, use ResultSet#getTimestamp() instead.

Date date = resultSet.getTimestamp("columnname");
// ...

It returns java.sql.Timestamp which is a subclass of java.util.Date, so the upcast is safe.

like image 178
BalusC Avatar answered Oct 27 '22 12:10

BalusC