ChatGPT解决这个技术问题 Extra ChatGPT

Difference between oracle DATE and TIMESTAMP

What are the difference between Oracle DATE and TIMESTAMP type? Both have date and time component? Also what is corresponding type in Java for these date types?

TIMESTAMP is the same as DATE, except it has added fractional seconds precision.
The biggest difference: DATE is accurate to the second and doesn't have fractional seconds. TIMESTAMP has fractional seconds. The number of decimal places in the seconds depends on the server OS, for example the Oracle on my Windows 7 machine returns three decimal places for the timestamp whereas a client's huge Solaris box returns six. Timestamps can also hold a specific time zone or be normalized to common time zone - go here and then search for "TIMESTAMP" for more information, then experiment a little :)

G
Guillermo Luque

DATE and TIMESTAMP have the same size (7 bytes). Those bytes are used to store century, decade, year, month, day, hour, minute and seconds. But TIMESTAMP allows to store additional info such as fractional seconds (11 bytes) and fractional seconds with timezone (13 bytes).

TIMESTAMP was added as an ANSI compliant to Oracle. Before that, it had DATE only.

In general cases you should use DATE. But if precision in time is a requirement, use TIMESTAMP.

And about Java, the oracle.sql.DATE class from Oracle JDBC driver, provides conversions between the Oracle Date/Timestamp data type and Java classes java.sql.Date, java.sql.Time and java.sql.Timestamp.


A word of warning: unfortunately it appears that, by default, when you query a DATE column in Oracle, it returns you only the "day", but if you cast it like TO_TIMESTAMP(DATE_COLUMN_NAME) then it returns you more precision. Which somehow isn't the default with jdbc/hibernate, at least it isn't here.
"In general cases you should use DATE" -- but why, exactly?
You should use TIMESTAMP WITH TIME ZONE. Otherwise daylight saving time will introduce ambiguous times.
I find it really confusing, that DATE type contains TIME information. That's not what the word means.
@Daddy32 TIMESTAMP was added about 20 years after DATE. They couldn't really go back and change DATE.
M
Marcin Badtke

https://i.stack.imgur.com/yOVlg.png

https://i.stack.imgur.com/0XHtB.png


Why do you use so many illustrations in your post instead of writing it out?
@mast because this is what I want to do - illustrate the problem/solution. This is what humans are used to - pictures. It is natural and easier to remember.
@MarcinBadtke Really appreciate your effort!