M
M
MaxLich2017-11-10 16:16:57
PostgreSQL
MaxLich, 2017-11-10 16:16:57

How to store date in PostgreSQL database?

Hello. I receive a date in the form of a long number from another program. I need to save this date in the database. So far, I quickly did it to save it as a string, like this:

String dateTime = new Date(event.getCreateTime()).toInstant().toString();

It turns out that the date is immediately saved in a readable format, and there are no problems with the output. But sorting doesn't work that way, and it's generally wrong.
Tried different things (Instant, Timestamp, LocalDateTime, java, util.Date, java.sql.Date), nothing helps. The conclusion goes with the
70th year (the time coincides).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxLich, 2017-11-10
@MaxLich

Solved this problem. It turned out that it was my jamb: I did not notice, and set the column type to "time with time zone", but it was necessary - "timestamp with time zone". I now perform the conversion of the incoming date like this:

long createTime = event.getCreateTime();
preparedStatement.setTimestamp(column, new java.sql.Timestamp(createTime), Calendar.newInstance());

(But I'm thinking of converting to the DateTime API from Java 8.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question