一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Mysql - 解析MySql與Java的時間類型

解析MySql與Java的時間類型

2020-04-25 16:46MYSQL教程網 Mysql

本篇文章是對MySql與Java的時間類型進行了詳細的分析介紹,需要的朋友參考下

MySql的時間類型有          Java中與之對應的時間類型
date                                           java.sql.Date
Datetime                                    java.sql.Timestamp
Timestamp                                  java.sql.Timestamp
Time                                          java.sql.Time
Year                                           java.sql.Date

對其進行分析
參考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 的方式進行該類字段的顯示。添加該類字段數據,即可以使用字符串類型,也可以使用數字類型

由于Date類型的字段只記錄日期信息,所以如果添加的數據中包含了時間信息,該時間信息將會自動被截斷。
如果要保存時間信息,可以考慮使用DateTime類型。
經過測試,發現如下2種方式可以對Date類型字段進行填充:
按字符串:
insert into time_table(CreateDate) values(‘2007-04-09')
按數字:
insert into time_table(CreateDate) values(20070409)
獲取可以用java.sql.Date類型獲取
代碼為:
Date dtDate =rsBuffer.getDate("CreateDate");
測試代碼如下:(其中,IDBFace 是自己基于JDBC封裝的一個簡單類, 接受Sql對數據庫進行操作)

復制代碼 代碼如下:


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();
}


執行結果: 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最主要的區別在于:DateTime 可以記錄日期和時間信息。而Date只記錄日期信息。表示范圍為: 1000-01-01 00:00:00 至 9999-12-31 23:59:59 MySql的按照YYYY-MM-DD HH:MM:SS對數據進行格式化,允許以字符串和數字的方式提交。

例如以數字的方式進行提交:
insert into time_table(CreateDate) values(20070409132013)
獲取該類型的數據可以使用: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();
}


執行結果: 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中未對Timestamp類型的列賦值,該列將被構造成當前時間。
提交NULL值也會使該列以當前時間錄入。
如果時間提交錯誤,該列將被填入0.
Timestamp比DateTime 類型所需的存儲空間更小,只需要4個字節,而DateTime需要8個字節。
但是有一點需要特別注意。Timestamp只能表示時間范圍為1970 -2037.
使用Timestamp一定要確保提交的時間數據一定不會超過這個范圍。
代碼與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只記錄時間信息,不包含日期信息。
范圍為-838:59:59 到 838:59:59, MySql 以HH:MM:SS格式化該數據,允許輸入為字符串或者數字。
代碼:

復制代碼 代碼如下:


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();
       }


執行結果: 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位的。
默認情況是4位。其范圍為1901-2155
2位的表述法只記錄后2位 。其范圍為1970-2069
允許以字符串或者數字的方式插入。
代碼:

復制代碼 代碼如下:


       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();
       }


執行結果:2007
需要說明的是:
Date.getYear()方法返回至1900年起經過了多少年。所以為了顯示正確的時間,必須加上1900.
該方法已經被廢棄。

另外。
有一種方法是:不使用上述任何一種類型來記錄時間。
而是以char(或vchar)的方式來記錄時間。
這樣做在插入數據和顯示記錄的時候固然不用進行任何轉換而比較方便。
但是要承擔2個重要的缺陷。
(1) 要單獨開發方法驗證時間數據的合法性。例如ajidjieoa字符串不是一個時間信息,但仍然可以正常插入。
(2) 如果系統需將時間范圍做為條件進行記錄檢索。這也會是一個大麻煩。用字符串記錄時間將無法使用MySql為時間提供的API.對時間范圍檢索的代碼可能與數據庫剝離。這樣對性能必然造成影響。例如,要從100萬條數據中查詢時間范圍為1992-3-12 –1992-3-13日的區區100條數據,你可能不得不將100萬條數據都查出來,再開發新的方法進行過濾。

另外,MySql到4.1時間精度貌若只到秒。
要記錄更細的時間粒度。可以考慮構造DateTime.
記錄DateTime.trick().
這只是一個想法,有沒有額外的問題尚未證明。/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 变态 另类 人妖小说 | 深夜福利影院 | 日韩理论片在线看免费观看 | 无人区乱码1区2区3区网站 | 日韩欧美中文字幕一区 | 国产美女久久精品香蕉69 | 关晓彤被调教出奶水的视频 | 男男18视频免费网站 | 免费国产成人高清视频网站 | 亚洲精品91在线 | 惊弦45集免费看 | 国产日韩一区二区三区 | 午夜伦午夜伦锂电影 | 偷拍综合网 | 爱爱亚洲| 午夜AV亚洲一码二中文字幕青青 | 成年人免费在线看的惊悚动作片 | 二次元美女挤奶漫画 | 日日免费视频 | 天堂久久久久va久久久久 | 爱爱小视频免费看 | 亚洲国产精品久久久久久网站 | 四虎在线播放 | 免费视频专区一国产盗摄 | 97综合| 门房秦大爷最新章节阅读 | 日韩精品特黄毛片免费看 | 欧式午夜理伦三级在线观看 | 精品免费视在线观看 | 欧美日本一本线在线观看 | 亚洲v日韩v欧美在线观看 | 国产亚洲一区二区三区 | 亚洲人成网站在线观看妞妞网 | 日韩欧美精品一区二区 | 国产在线麻豆波多野结衣 | 麻豆自拍| 1024人成网站色 | 奇米影视先锋 | 国产自在线观看 | 国产高清国内精品福利 | 久久黄色精品视频 |