解析MySql與Java的時(shí)間類型
更新時(shí)間:2013年06月22日 16:54:37 作者:
本篇文章是對(duì)MySql與Java的時(shí)間類型進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
MySql的時(shí)間類型有 Java中與之對(duì)應(yīng)的時(shí)間類型
date java.sql.Date
Datetime java.sql.Timestamp
Timestamp java.sql.Timestamp
Time java.sql.Time
Year java.sql.Date
對(duì)其進(jìn)行分析
參考MySql 的reference manual
Date:
A date. The supported range is '1000-01-01' to '9999-12-31'. MySQL displays DATE values in 'YYYY-MM-DD' format, but allows you to assign values to DATE columns using either strings or numbers.
只記錄日期信息,表示范圍為1000-01-01 至 9999-12-31。
MySql 按照YYYY-MM-DD 的方式進(jìn)行該類字段的顯示。添加該類字段數(shù)據(jù),即可以使用字符串類型,也可以使用數(shù)字類型
由于Date類型的字段只記錄日期信息,所以如果添加的數(shù)據(jù)中包含了時(shí)間信息,該時(shí)間信息將會(huì)自動(dòng)被截?cái)唷?BR>如果要保存時(shí)間信息,可以考慮使用DateTime類型。
經(jīng)過(guò)測(cè)試,發(fā)現(xiàn)如下2種方式可以對(duì)Date類型字段進(jìn)行填充:
按字符串:
insert into time_table(CreateDate) values(‘2007-04-09')
按數(shù)字:
insert into time_table(CreateDate) values(20070409)
獲取可以用java.sql.Date類型獲取
代碼為:
Date dtDate =rsBuffer.getDate("CreateDate");
測(cè)試代碼如下:(其中,IDBFace 是自己基于JDBC封裝的一個(gè)簡(jiǎn)單類, 接受Sql對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作)
public void testDate()throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateDate) values(20070409)";
DBFace.update(strInsert);
//獲取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Date dtDate =rsBuffer.getDate("CreateDate");
System.out.println(dtDate.toString());
}
DBFace.close();
}
執(zhí)行結(jié)果: 2007-04-09
DateTime
A date and time combination. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. MySQL displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format, but allows you to assign values to DATETIME columns using either strings or numbers.
DateTime 與Date最主要的區(qū)別在于:DateTime 可以記錄日期和時(shí)間信息。而Date只記錄日期信息。表示范圍為: 1000-01-01 00:00:00 至 9999-12-31 23:59:59 MySql的按照YYYY-MM-DD HH:MM:SS對(duì)數(shù)據(jù)進(jìn)行格式化,允許以字符串和數(shù)字的方式提交。
例如以數(shù)字的方式進(jìn)行提交:
insert into time_table(CreateDate) values(20070409132013)
獲取該類型的數(shù)據(jù)可以使用:java.sql.Timestamp類型
代碼如下:
public void testDateTime() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateDateTime) values(20070409132013)";
DBFace.update(strInsert);
//獲取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Timestamp tsBuffer =rsBuffer.getTimestamp("CreateDateTime");
System.out.println(tsBuffer.toString());
}
DBFace.close();
}
執(zhí)行結(jié)果: 2007-04-09 13:20:13.0
TimeStamp
A timestamp. The range is '1970-01-01 00:00:00' to partway through the year 2037. A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation. The first TIMESTAMP column in a table is automatically set to the date and time of the most recent operation if you don't assign it a value yourself. You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value.
與DateTime類型非常相似
范圍為1970-01-01 –2037年,精度為1秒/
如果在Sql中未對(duì)Timestamp類型的列賦值,該列將被構(gòu)造成當(dāng)前時(shí)間。
提交NULL值也會(huì)使該列以當(dāng)前時(shí)間錄入。
如果時(shí)間提交錯(cuò)誤,該列將被填入0.
Timestamp比DateTime 類型所需的存儲(chǔ)空間更小,只需要4個(gè)字節(jié),而DateTime需要8個(gè)字節(jié)。
但是有一點(diǎn)需要特別注意。Timestamp只能表示時(shí)間范圍為1970 -2037.
使用Timestamp一定要確保提交的時(shí)間數(shù)據(jù)一定不會(huì)超過(guò)這個(gè)范圍。
代碼與DateTime類是,而且我不喜歡用,所以略掉了。
Time:
A time. The range is '-838:59:59' to '838:59:59'. MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME columns using either strings or numbers.
Time只記錄時(shí)間信息,不包含日期信息。
范圍為-838:59:59 到 838:59:59, MySql 以HH:MM:SS格式化該數(shù)據(jù),允許輸入為字符串或者數(shù)字。
代碼:
public void testTime() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateTime) values(131211)";
DBFace.update(strInsert);
//獲取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Time tmBuffer =rsBuffer.getTime("CreateTime");
System.out.println(tmBuffer.toString());
}
DBFace.close();
}
執(zhí)行結(jié)果: 13:12:11
Year
A year in two-digit or four-digit format. The default is four-digit format. In four-digit format, the allowable values are 1901 to 2155, and 0000. In two-digit format, the allowable values are 70 to 69, representing years from 1970 to 2069. MySQL displays YEAR values in YYYY format, but allows you to assign values to YEAR columns using either strings or numbers. The YEAR type is unavailable prior to MySQL 3.22.
Year可以有2種表示方式,4位的和2位的。
默認(rèn)情況是4位。其范圍為1901-2155
2位的表述法只記錄后2位 。其范圍為1970-2069
允許以字符串或者數(shù)字的方式插入。
代碼:
public void testYear() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateYear) values(2007)";
DBFace.update(strInsert);
//獲取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Date dtBuffer =rsBuffer.getDate("CreateYear");
System.out.println(dtBuffer.getYear()+1900);
}
DBFace.close();
}
執(zhí)行結(jié)果:2007
需要說(shuō)明的是:
Date.getYear()方法返回至1900年起經(jīng)過(guò)了多少年。所以為了顯示正確的時(shí)間,必須加上1900.
該方法已經(jīng)被廢棄。
另外。
有一種方法是:不使用上述任何一種類型來(lái)記錄時(shí)間。
而是以char(或vchar)的方式來(lái)記錄時(shí)間。
這樣做在插入數(shù)據(jù)和顯示記錄的時(shí)候固然不用進(jìn)行任何轉(zhuǎn)換而比較方便。
但是要承擔(dān)2個(gè)重要的缺陷。
(1) 要單獨(dú)開發(fā)方法驗(yàn)證時(shí)間數(shù)據(jù)的合法性。例如ajidjieoa字符串不是一個(gè)時(shí)間信息,但仍然可以正常插入。
(2) 如果系統(tǒng)需將時(shí)間范圍做為條件進(jìn)行記錄檢索。這也會(huì)是一個(gè)大麻煩。用字符串記錄時(shí)間將無(wú)法使用MySql為時(shí)間提供的API.對(duì)時(shí)間范圍檢索的代碼可能與數(shù)據(jù)庫(kù)剝離。這樣對(duì)性能必然造成影響。例如,要從100萬(wàn)條數(shù)據(jù)中查詢時(shí)間范圍為1992-3-12 –1992-3-13日的區(qū)區(qū)100條數(shù)據(jù),你可能不得不將100萬(wàn)條數(shù)據(jù)都查出來(lái),再開發(fā)新的方法進(jìn)行過(guò)濾。
另外,MySql到4.1時(shí)間精度貌若只到秒。
要記錄更細(xì)的時(shí)間粒度??梢钥紤]構(gòu)造DateTime.
記錄DateTime.trick().
這只是一個(gè)想法,有沒有額外的問(wèn)題尚未證明。/
date java.sql.Date
Datetime java.sql.Timestamp
Timestamp java.sql.Timestamp
Time java.sql.Time
Year java.sql.Date
對(duì)其進(jìn)行分析
參考MySql 的reference manual
Date:
A date. The supported range is '1000-01-01' to '9999-12-31'. MySQL displays DATE values in 'YYYY-MM-DD' format, but allows you to assign values to DATE columns using either strings or numbers.
只記錄日期信息,表示范圍為1000-01-01 至 9999-12-31。
MySql 按照YYYY-MM-DD 的方式進(jìn)行該類字段的顯示。添加該類字段數(shù)據(jù),即可以使用字符串類型,也可以使用數(shù)字類型
由于Date類型的字段只記錄日期信息,所以如果添加的數(shù)據(jù)中包含了時(shí)間信息,該時(shí)間信息將會(huì)自動(dòng)被截?cái)唷?BR>如果要保存時(shí)間信息,可以考慮使用DateTime類型。
經(jīng)過(guò)測(cè)試,發(fā)現(xiàn)如下2種方式可以對(duì)Date類型字段進(jìn)行填充:
按字符串:
insert into time_table(CreateDate) values(‘2007-04-09')
按數(shù)字:
insert into time_table(CreateDate) values(20070409)
獲取可以用java.sql.Date類型獲取
代碼為:
Date dtDate =rsBuffer.getDate("CreateDate");
測(cè)試代碼如下:(其中,IDBFace 是自己基于JDBC封裝的一個(gè)簡(jiǎn)單類, 接受Sql對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作)
復(fù)制代碼 代碼如下:
public void testDate()throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateDate) values(20070409)";
DBFace.update(strInsert);
//獲取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Date dtDate =rsBuffer.getDate("CreateDate");
System.out.println(dtDate.toString());
}
DBFace.close();
}
執(zhí)行結(jié)果: 2007-04-09
DateTime
A date and time combination. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. MySQL displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format, but allows you to assign values to DATETIME columns using either strings or numbers.
DateTime 與Date最主要的區(qū)別在于:DateTime 可以記錄日期和時(shí)間信息。而Date只記錄日期信息。表示范圍為: 1000-01-01 00:00:00 至 9999-12-31 23:59:59 MySql的按照YYYY-MM-DD HH:MM:SS對(duì)數(shù)據(jù)進(jìn)行格式化,允許以字符串和數(shù)字的方式提交。
例如以數(shù)字的方式進(jìn)行提交:
insert into time_table(CreateDate) values(20070409132013)
獲取該類型的數(shù)據(jù)可以使用:java.sql.Timestamp類型
代碼如下:
復(fù)制代碼 代碼如下:
public void testDateTime() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateDateTime) values(20070409132013)";
DBFace.update(strInsert);
//獲取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Timestamp tsBuffer =rsBuffer.getTimestamp("CreateDateTime");
System.out.println(tsBuffer.toString());
}
DBFace.close();
}
執(zhí)行結(jié)果: 2007-04-09 13:20:13.0
TimeStamp
A timestamp. The range is '1970-01-01 00:00:00' to partway through the year 2037. A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation. The first TIMESTAMP column in a table is automatically set to the date and time of the most recent operation if you don't assign it a value yourself. You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value.
與DateTime類型非常相似
范圍為1970-01-01 –2037年,精度為1秒/
如果在Sql中未對(duì)Timestamp類型的列賦值,該列將被構(gòu)造成當(dāng)前時(shí)間。
提交NULL值也會(huì)使該列以當(dāng)前時(shí)間錄入。
如果時(shí)間提交錯(cuò)誤,該列將被填入0.
Timestamp比DateTime 類型所需的存儲(chǔ)空間更小,只需要4個(gè)字節(jié),而DateTime需要8個(gè)字節(jié)。
但是有一點(diǎn)需要特別注意。Timestamp只能表示時(shí)間范圍為1970 -2037.
使用Timestamp一定要確保提交的時(shí)間數(shù)據(jù)一定不會(huì)超過(guò)這個(gè)范圍。
代碼與DateTime類是,而且我不喜歡用,所以略掉了。
Time:
A time. The range is '-838:59:59' to '838:59:59'. MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME columns using either strings or numbers.
Time只記錄時(shí)間信息,不包含日期信息。
范圍為-838:59:59 到 838:59:59, MySql 以HH:MM:SS格式化該數(shù)據(jù),允許輸入為字符串或者數(shù)字。
代碼:
復(fù)制代碼 代碼如下:
public void testTime() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateTime) values(131211)";
DBFace.update(strInsert);
//獲取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Time tmBuffer =rsBuffer.getTime("CreateTime");
System.out.println(tmBuffer.toString());
}
DBFace.close();
}
執(zhí)行結(jié)果: 13:12:11
Year
A year in two-digit or four-digit format. The default is four-digit format. In four-digit format, the allowable values are 1901 to 2155, and 0000. In two-digit format, the allowable values are 70 to 69, representing years from 1970 to 2069. MySQL displays YEAR values in YYYY format, but allows you to assign values to YEAR columns using either strings or numbers. The YEAR type is unavailable prior to MySQL 3.22.
Year可以有2種表示方式,4位的和2位的。
默認(rèn)情況是4位。其范圍為1901-2155
2位的表述法只記錄后2位 。其范圍為1970-2069
允許以字符串或者數(shù)字的方式插入。
代碼:
復(fù)制代碼 代碼如下:
public void testYear() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateYear) values(2007)";
DBFace.update(strInsert);
//獲取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Date dtBuffer =rsBuffer.getDate("CreateYear");
System.out.println(dtBuffer.getYear()+1900);
}
DBFace.close();
}
執(zhí)行結(jié)果:2007
需要說(shuō)明的是:
Date.getYear()方法返回至1900年起經(jīng)過(guò)了多少年。所以為了顯示正確的時(shí)間,必須加上1900.
該方法已經(jīng)被廢棄。
另外。
有一種方法是:不使用上述任何一種類型來(lái)記錄時(shí)間。
而是以char(或vchar)的方式來(lái)記錄時(shí)間。
這樣做在插入數(shù)據(jù)和顯示記錄的時(shí)候固然不用進(jìn)行任何轉(zhuǎn)換而比較方便。
但是要承擔(dān)2個(gè)重要的缺陷。
(1) 要單獨(dú)開發(fā)方法驗(yàn)證時(shí)間數(shù)據(jù)的合法性。例如ajidjieoa字符串不是一個(gè)時(shí)間信息,但仍然可以正常插入。
(2) 如果系統(tǒng)需將時(shí)間范圍做為條件進(jìn)行記錄檢索。這也會(huì)是一個(gè)大麻煩。用字符串記錄時(shí)間將無(wú)法使用MySql為時(shí)間提供的API.對(duì)時(shí)間范圍檢索的代碼可能與數(shù)據(jù)庫(kù)剝離。這樣對(duì)性能必然造成影響。例如,要從100萬(wàn)條數(shù)據(jù)中查詢時(shí)間范圍為1992-3-12 –1992-3-13日的區(qū)區(qū)100條數(shù)據(jù),你可能不得不將100萬(wàn)條數(shù)據(jù)都查出來(lái),再開發(fā)新的方法進(jìn)行過(guò)濾。
另外,MySql到4.1時(shí)間精度貌若只到秒。
要記錄更細(xì)的時(shí)間粒度??梢钥紤]構(gòu)造DateTime.
記錄DateTime.trick().
這只是一個(gè)想法,有沒有額外的問(wèn)題尚未證明。/
相關(guān)文章
php實(shí)現(xiàn)三級(jí)級(jí)聯(lián)下拉框
這篇文章主要介紹了php實(shí)現(xiàn)三級(jí)級(jí)聯(lián)下拉框,上網(wǎng)翻找了許多三級(jí)級(jí)聯(lián)下拉框?qū)崿F(xiàn)的資料,下面分享給大家2016-04-04PHP實(shí)現(xiàn)防重復(fù)提交(防抖)的方法總結(jié)
當(dāng)涉及到處理表單提交或用戶點(diǎn)擊按鈕等操作時(shí),防抖(Debounce)是一種重要的技術(shù),它可以有效地防止不必要的重復(fù)操作,本文為大家整理了 PHP 中防抖的多種實(shí)現(xiàn)方法,需要的可以參考下2023-09-09php 實(shí)現(xiàn)賬號(hào)不能同時(shí)登陸的方法分析【當(dāng)其它地方登陸時(shí),當(dāng)前賬號(hào)失效】
這篇文章主要介紹了php 實(shí)現(xiàn)賬號(hào)不能同時(shí)登陸的方法,結(jié)合實(shí)例形式分析了PHP基于session實(shí)現(xiàn)當(dāng)其它地方登陸時(shí),當(dāng)前賬號(hào)失效的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03PHP面向?qū)ο蟪绦蛟O(shè)計(jì)子類擴(kuò)展父類(子類重新載入父類)操作詳解
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)子類擴(kuò)展父類(子類重新載入父類)操作,涉及php面向?qū)ο蠓椒ㄖ貙懪c擴(kuò)展相關(guān)操作技巧,需要的朋友可以參考下2019-06-06PHP定時(shí)任務(wù)獲取微信access_token的方法
這篇文章主要介紹了PHP定時(shí)任務(wù)獲取微信access_token的方法,涉及php基于curl動(dòng)態(tài)獲取access_token及CentOS下crontab設(shè)置計(jì)劃任務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10php程序內(nèi)部post數(shù)據(jù)的方法
這篇文章主要介紹了php程序內(nèi)部post數(shù)據(jù)的方法,涉及curl的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03php實(shí)現(xiàn)簡(jiǎn)單文件下載的方法
這篇文章主要介紹了php實(shí)現(xiàn)簡(jiǎn)單文件下載的方法,以實(shí)例形式簡(jiǎn)單分析了文件下載的原理與實(shí)現(xiàn)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-01-01