Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JDBC, when to use Time, Date, and Timestamp

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?

like image 664
mcherm Avatar asked Jun 04 '09 19:06

mcherm


People also ask

What is the difference between TIMESTAMP and datetime in Java?

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'.

Why TIMESTAMP is used in Java?

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.

What is the difference between TIMESTAMP and datetime in sql Server?

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.

Why TIMESTAMP is used in sql?

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.


1 Answers

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.

  • ex: January 1, 2003

Use Time if you are only interested in the time portion of the date string

  • ex: 2:00pm

Use Timestamp if you want the date and time of the date string

  • ex: January 1, 2003 2:00pm
like image 88
Mr. Will Avatar answered Oct 22 '22 16:10

Mr. Will