mysql和java的時間對應關係

來源:互聯網
上載者:User

標籤:java se   包含   版本   lan   mysq   tle   簡單   ini   util   

引用:http://blog.csdn.net/xinghuo0007/article/details/51500923

 

MySQL(版本:5.1.50)的時間日期類型如下:

datetime 8bytes xxxx-xx-xx xx:xx:xx 1000-01-01 00:00:00到9999-12-31 23:59:59
timestamp 4bytes xxxx-xx-xx xx:xx:xx 1970-01-01 00:00:01到2038
date 3bytes xxxx-xx-xx 1000-01-01到9999-12-31
year 1bytes xxxx 1901到2155
time 3bytes xx:xx:xx -838:59:59到838:59:59(為了滿足時間的加減運算)

 

 

Java(1.6) 中能儲存時間日期類型的類主要有

Java.util.Date

java.util.Calendar

java.sql.Date

java.sql.Time

java.sql.Timestamp

 

      以前從MySQL中查詢出來的時間日期類型,都放在java.util.Date類型裡面了。這樣帶來一系列的問題,首先這個類提供的時間操作函數太少,一般都需要轉換成java.util.Calendar再去操作;其次即使使用了java.util.Calendar,也不是很方便,一個很簡單的想法,需要寫很多代碼才能實現;java.util.Date的資料內容為xxxx-xx-xx xx:xx:xx,有時候不需要時間,只需要日期。從資料庫中取值出來的日期類型放到這個類中的時候,會在時間位自動補上目前時間。這使得本來兩個日期在資料庫中是相等的,取出來放到這個類得時候就不再相等了,需要去考慮時間上的誤差,很是頭疼。

 

java提供與mysql方便互動的三種資料類型

 

java.sql.Date

java.sql.Time

java.sql.Timestamp

它們都是繼承java.util.Date,算是對該類的精簡,很適合跟資料庫互動。

 

===========java注入資料庫==========

java類型   mysql類型        成功與否
date         date               yes
date         time               no
date         timestamp       no
date         datetime         no

time         date               no
time         time               yes
time         timestamp       no
time         datetime         no

timestamp date              yes
timestamp time              yes
timestamp timestamp     yes
timestamp datetime        yes
==========end java注入資料庫========
總規律,如果A完全包含B,則A可以向B注入資料,否則報錯


 

==========從資料庫提取到java ==========

mysql類型    java類型     成與否
date             date         yes
date             time         yes --------------缺少的部分使用曆元
date           timestamp   yes --------------缺少的部分使用曆元  

time           date           yes --------------缺少的部分使用曆元
time           time           yes
time          timestamp    yes --------------缺少的部分使用曆元

timestamp date           yes
timestamp time           yes
timestamp timestamp   yes

datetime      date         yes
datetime      time         yes
datetime    timestamp   yes
==========end 從資料庫提取到java=======
不會出錯,缺少的部分使用曆元,而不是當前日期時間

 

 

 

 

 


null to db(null) =====> 也是null  
null to db(not null)=======> 資料庫報錯
db(null) to java==========> 如果單欄位出來,則整個entity都是null,如果帶著其他不是null的欄位出來,則可以執行個體化entity,本身欄位依然是null
db(not null) to java==========> 如果包含日期,則報錯,否則為000
最優解決方案,定義成可以為null

java.sql時間系統的運算系列

after,before
compareTo原小於參數返回<0,等於返回=0,大於返回>0

優點:於資料庫同類型,可以方便傳輸(無論是從DB到src還是反方向),方便比較大小
缺點:缺少運算單元,不適合時間跳躍的運算和間隔的運算

總結:calendar具有強大的跳躍運算和間隔運算能力,在需要的時候,可以將sql系列的時間轉成calendar。
先設定calendar為曆元,然後從sql系列時間中轉換,最後再轉回sql系列時間。
calendar只用於時間有跳躍的轉換,對比運算統一使用sql系統,這樣代碼將更清晰

 

 

 

 

date 和 calendar怎麼初始化為格林威治時間

new date(0)
calendar.setTimeInMillis(0)


sql系列時間

static valueOf


new XX(0)獲得曆元
new XX(year+1900, month+1,day,hour,minute,second,nano)已淘汰,建立也沒錯

toString或者SimpleDateFormat



文章來源: http://blog.csdn.NET/weinianjie1/article/details/6310770

mysql和java的時間對應關係

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.