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"