JDBC offers me 3 different datatypes for time-related fields: "Date", "Time" and "Timestamp". Can someone provide a simple summary of what each one is used for and how to choose which to use for a given problem?
Range − Datetime data type supports a date along with time in the range between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. But timestamp data type supports a date along with time in the range between '1970-01-01 00:00:01' to '2038-01-19 08:44:07'.
A Timestamp also provides formatting and parsing operations to support the JDBC escape syntax for timestamp values. The precision of a Timestamp object is calculated to be either: 19 , which is the number of characters in yyyy-mm-dd hh:mm:ss. 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.
The Timestamp in SQL allows us to store and perform our operations with both the dates and time values, and that too without any particular time zone usually. The TIMESTAMP() function in SQL allows us to return and represent any DateTime expression, from any date or any DateTime expression.
Let's assume you have the date/time January 1, 2003 2:00pm stored in a database column. The three options are used as follows:
Use Date if you are only interested in the date portion of the date string.
Use Time if you are only interested in the time portion of the date string
Use Timestamp if you want the date and time of the date string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With