MySQL database time type stored in Java "date, hour, datetime, timestamp"

Source: Internet
Author: User

In MySQL for the time of the storage of their own table is set when the varchar type, it feels very convenient.

Yesterday took someone else to build a table to write code, found that the time type of the table is DateTime, with the feeling of a try not to, on-line check the beginning of a number of tests are not right, a face Meng forced.

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------




, the datetime type data in MySQL corresponds to the timestamp class in Java.

Date: Only dates, no time, 2016-09-21;

Time: There are only times, no dates, 23:42:31;

DateTime: Date and time, 2016-09-21 23:42:31.

Timestamp: You can automatically insert the time for you when inserting or update, time format: 2016-09-21 23:42:31.


I used navicat to find a table. Added a timestamp field, as follows:



Export table structure see this field SQL is: ' time_test ' timestamp null DEFAULT null on UPDATE current_timestamp,


To get to the fact that this database is using the datetime type, the method of generating the time data stored in Java is as follows:

1 2 3 4
Date date = new Date (); Timestamp TIMESTAMEP = new Timestamp (Date.gettime ()); System.out.println (TIMESTAMEP);//output result: 2016-09-22 10:38:14.964
code slices from codesSnippet_file_0.txt
can see the results also with milliseconds this can be stored directly into the database automatically removed milliseconds, in addition to the database in the non-millisecond time, in Java from the database to get that time is also with milliseconds, only milliseconds is 0.

MySQL database time type stored in Java "date, hour, datetime, timestamp"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.