Today, I reviewed and shared the overall time types and methods of oracle, mysql, and h2, hoping to help everyone
Oracle
time type:
1.DATE : can store month, year, day, century, hour, minute and second, the functions have to_char() and to_date();
2.TIMESTAMP : It includes the information of the year, month, day, hour, minute and second of all DATE data types, and includes the information of the fractional second. If you want to convert a DATE type to a TIMESTAMP type, use the CAST function.
When inserting a time type time into an oracle database through JDBC, you can directly insert a java.sql.Date type time into the database, but the time only includes the year, month, and day, not hours, minutes, and seconds. If you need to insert the time of type java.util.Date into oracle through JDBC, first convert the Date into a formatted string, and then insert it. (If you use java.sql.timestamp and timestamp in oracle, you can directly access and include hours, minutes and seconds)
Insert the formatted date sql statement into the database as follows:
insert into tab_demo values (to_date(格式化日期,'yyyy-mm-dd hh24:mi:ss'));
MySQL
time type:
DATETIME
DATETIME is used to represent the year, month, day, hour, minute, and second. It is a combination of DATE and TIME, and the recorded year (see the table above) is relatively long. If there is such a requirement in practical applications, the DATETIME type can be used.
TIMESTAMP
- TIMESTAMP is used to represent the year, month, day, hour, minute and second, but the recorded year (see the table above) is relatively short.
- TIMESTAMP is related to the time zone and better reflects the current time. When inserting a date, it will be converted to the local time zone before storing; when querying a date, the date will be converted to the local time zone and then displayed. So people in different time zones see the same time differently.
- The first TIMESTAMP column in the table is automatically set to the system time (CURRENT_TIMESTAMP). When inserting or updating a row without explicitly assigning a value to the TIMESTAMP column, it is also automatically set to the current system time. If there is a second TIMESTAMP column in the table, the default value is set to 0000-00-00 00:00:00.
- The properties of TIMESTAMP are greatly affected by Mysql version and server SQLMode.
If the recorded date needs to be used by people in different time zones, it is best to use TIMESTAMP.
DATE
DATE is used to represent the year, month and day. If the actual application value needs to save the year, month and day, DATE can be used.
TIME
TIME is used to represent hours, minutes and seconds. If the actual application value needs to save hours, minutes and seconds, TIME can be used.
YEAR
YEAR is used to represent the year, YEAR has 2 digits (preferably 4 digits) and the year in 4 digit format. The default is 4 bits. If the actual application only saves the year, it is perfectly fine to store the YEAR type with 1 bytes. It can not only save storage space, but also improve the operation efficiency of the table.
function:
date_format(date, format) function, MySQL date format function date_format() time to string
unix_timestamp() function to convert timestamp
str_to_date(str, format) function String to time
from_unixtime(unix_timestamp, format) function, MySQL timestamp format function from_unixtime
time to string
select date_format(now(), '%Y-%m-%d');
#Result: 2016-01-05
time to timestamp
select unix_timestamp(now());
#result: 1452001082
String to time
select str_to_date('2016-01-02', '%Y-%m-%d %H');
#结果:2016-01-02 00:00:00
字符串转时间戳
select unix_timestamp('2016-01-02');
#结果:1451664000
时间戳转时间
select from_unixtime(1451997924);
#结果:2016-01-05 20:45:24
时间戳转字符串
select from_unixtime(1451997924,'%Y-%d');
#结果:2016-01-05 20:45:24
H2
时间类型:
(时间)TIME:
格式为 hh:mm:ss.对应到Java类型:java.sql.Time.
(日期)DATE:
格式为 yyyy-MM-dd.对应到Java类型: java.sql.Date
(时间戳)TIMESTAMP
{ TIMESTAMP | DATETIME | SMALLDATETIME }
格式为 yyyy-MM-dd hh:mm:ss[.nnnnnnnnn].
对应到Java类型: java.sql.Timestamp (java.util.Date 也支持).
函数:
如果要想将一个字符串格式的日期转化为日期格式的日期,需要使用PARSEDATETIME(string,formatstring,时区)函数
e.g.
INSERT INTO TEST (ID, DATE) VALUES(1, parsedatetime('17-09-2012 18:47:52.69', 'dd-MM-yyyy hh:mm:ss.SS'))
或者 INSERT INTO TEST VALUES(2, {ts '2012-09-17 18:47:52.69'});
参考:
http://www.runoob.com/mysql/mysql-data-types.html
https://cloud.tencent.com/developer/ask/96009
如有错误或不足,望路过大佬海涵和指点