I have a java application that uses Spring JDBC to store data into Oracle. The data includes timestamps/dates which we use to query certain data in the future.
The date fields are defined like so in the DDL SQL file :
JobExecution {
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL
...
}
Java code to update these fields looks like so :
lastJobExecution.setEndTime(new Date(System.currentTimeMillis()));
new Date(System.currentTimeMillis())
stores current time in UTC format as per documentation below. The documentation of System.currentTimeMillis()
says that it returns the following :
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
The documentation of Date
says the following :
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
Params: date – the milliseconds since January 1, 1970, 00:00:00 GMT. See Also: System.currentTimeMillis()
However, when I connect to the oracle database using SQL developer, the date seems to be stored as per the local time and not UTC.
Questions :
- Does Oracle
TIMESTAMP
adjust the date as per the local time? I can't find this explicitly written anywhere in the Oracle documentation. - If yes what would be the best way to handle this. One way could be to convert this to UTC every time I read the data from this table. Another way could be to store it in the UTC format itself.
TIMESTAMP
stores the time information without further timezone information, i.e. it just stores whatever it is given. To store in the local timezone, we would useTIMESTAMP WITH LOCAL TIME ZONE
. – Turing85java.time.LocalDateTime
instead ofjava.util.Date
/java.sql.Timestamp
. – Mark Rotteveel