淺談Mybatis+mysql 存儲(chǔ)Date類型的坑
場(chǎng)景:
把一個(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ò)誤了
貼一下測(cè)試的一下渣碼
// 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. 測(cè)試毫秒數(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); // 將對(duì)象里時(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ù)庫中對(duì)應(yīng)字段的類型為Date
不顯示 時(shí)分秒 的情況:
Mapping文件中對(duì)應(yīng)字段的jdbcType為DATE類型
如果顯示時(shí)分秒的話,只需要將Mapping文件中對(duì)應(yīng)字段的類型改為TIMESTAMP即可.
以上這篇淺談Mybatis+mysql 存儲(chǔ)Date類型的坑就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Mysql故障排除:Starting MySQL. ERROR! Manager of pid-file quit without updating file2. MySQL插入數(shù)據(jù)時(shí),如果記錄不存在則insert,如果存在則update3. Oracle與Access表之間的導(dǎo)入和導(dǎo)出4. 講解SQL Server數(shù)據(jù)庫備份的多種方式5. 傳甲骨文將增加對(duì)MySQL投資與微軟競(jìng)爭(zhēng)6. 5步解決SQL Server只讀/正在恢復(fù)問題7. SQL Server根據(jù)查詢結(jié)果,生成XML文件8. 淺談MySql 視圖、觸發(fā)器以及存儲(chǔ)過程9. Oracle Optimizer:遷移到使用基于成本的優(yōu)化器-----系列1.210. 如何:創(chuàng)建和運(yùn)行 CLR SQL Server 用戶定義的函數(shù)
