淺談Mybatis+mysql 存儲(chǔ)Date類型的坑
場景:
把一個(gè)時(shí)間字符串轉(zhuǎn)成Date,存進(jìn)Mysql。時(shí)間天數(shù)會(huì)比實(shí)際時(shí)間少1天,也可能是小時(shí)少了13-14小時(shí)
Mysql的時(shí)區(qū)是CST(使用語句:show VARIABLES LIKE ’%time_zone%’; 查)
先放總結(jié):
修改方法:
1. 修改數(shù)據(jù)庫時(shí)區(qū)
2. 在jdbc.url里加后綴 &serverTimezone=GMT%2B8
3. 代碼里設(shè)置時(shí)區(qū),給SimpleDateFormat.setTimeZone(...)
例外:new Date() 可以直接存為正確時(shí)間,其他的不行。比如我試過,把new Date用sdf轉(zhuǎn)個(gè)2次,然后就錯(cuò)誤了
貼一下測試的一下渣碼
// 1.new Date()直接存數(shù)據(jù)庫則是正確的日期 結(jié)果:√ 190626,數(shù)據(jù)庫存儲(chǔ)正常// Date now = new Date(); // 2,new Date()用simpleDateFormat轉(zhuǎn)化為字符串再轉(zhuǎn)為Date。結(jié)果: × 少1天 190625// Date now1 = new Date();// String tempStr = yyMMddFormatter.format(now1);// String tempStrDate = tempStr.split(' ')[0];// 會(huì)加上00:00:00// Date date = yyMMddFormatter.parse(tempStrDate); // 3.配置文件加上&serverTimezone=GMT%2B8,√ 正確 // 4. 設(shè)置中國標(biāo)準(zhǔn)時(shí)區(qū) UTC+8 結(jié)果:√// SimpleDateFormat sdf = new SimpleDateFormat('yyMMdd'); // 設(shè)置時(shí)區(qū): 中國標(biāo)準(zhǔn)時(shí) China Standard Time UTC+08:00 使用GMT+8東8區(qū),結(jié)果:?使用默認(rèn)時(shí)區(qū)setTimeZone(TimeZone.getDefault);// sdf.setTimeZone(TimeZone.getTimeZone('UTC+8'));// System.out.println(sdf.getTimeZone().toString());// Date date = sdf.parse(liftMaxDt);// System.out.println(sdf.getTimeZone().toString());// System.out.println(date);//// Date targetDate = new Date(date.getTime());// System.out.println('------------------');// System.out.println(targetDate); // 5. 測試毫秒數(shù) new Date(ms);但是要先使用sdf轉(zhuǎn)入?yún)?結(jié)果:× 問題就在于SimpleDateFormat會(huì)混亂時(shí)區(qū)// SimpleDateFormat sdf = new SimpleDateFormat('yyMMdd');// Date date = sdf.parse(liftMaxDt);// Date targetDate = new Date(date.getTime());// System.out.println('使用sdf轉(zhuǎn)換date,在new Date(date.getTime())-----------');// System.out.println(targetDate); // 使用LocalDate.結(jié)果: × 還是少一天 DateTimeFormatter df = DateTimeFormatter.ofPattern('yyMMdd'); LocalDate ldt = LocalDate.parse(liftMaxDt, df); System.out.println('String類型的時(shí)間轉(zhuǎn)成LocalDateTime:'+ldt); // LocalDate轉(zhuǎn)LocalDateTime LocalDateTime lll = LocalDateTime.of(ldt, LocalTime.of(0,0,0)); ZoneId zone = ZoneId.systemDefault(); Instant instant = lll.atZone(zone).toInstant(); Date targetDate = Date.from(instant); // 將對象里時(shí)間屬性設(shè)置為String,數(shù)據(jù)庫里仍然用Date,用數(shù)據(jù)庫的時(shí)間函數(shù)轉(zhuǎn)化
最后,還是采用的數(shù)據(jù)庫為timestamp類型,用mysql的時(shí)間函數(shù)進(jìn)行轉(zhuǎn)換,保證時(shí)間為數(shù)據(jù)庫時(shí)間
補(bǔ)充知識(shí):mybatis解決java中的date類型存入oracle數(shù)據(jù)庫之后不顯示時(shí)分秒
實(shí)體類中類型為java.util.Date
private Date update_date;
數(shù)據(jù)庫中對應(yīng)字段的類型為Date
不顯示 時(shí)分秒 的情況:
Mapping文件中對應(yīng)字段的jdbcType為DATE類型
如果顯示時(shí)分秒的話,只需要將Mapping文件中對應(yīng)字段的類型改為TIMESTAMP即可.
以上這篇淺談Mybatis+mysql 存儲(chǔ)Date類型的坑就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Oracle數(shù)據(jù)庫PL/SQL過程調(diào)試的輸出方法2. SELECT...INTO的具體用法3. (轉(zhuǎn))Oracle RMAN快速入門指南4. Microsoft Office Access修改數(shù)據(jù)表名稱的方法5. MySql導(dǎo)出后再導(dǎo)入數(shù)據(jù)時(shí)出錯(cuò)問題6. Microsoft Office Access取消主鍵的方法7. DB2如何查看當(dāng)前用戶模式及切換用戶8. ORA-06512數(shù)字或值錯(cuò)誤字符串緩沖區(qū)太小異常詳解9. 用shell抽取,更新db2的數(shù)據(jù)10. MySQL算術(shù)/比較/邏輯/位/運(yùn)算符與正則舉例詳解
