淺談Mybatis+mysql 存儲(chǔ)Date類(lèi)型的坑
場(chǎng)景:
把一個(gè)時(shí)間字符串轉(zhuǎn)成Date,存進(jìn)Mysql。時(shí)間天數(shù)會(huì)比實(shí)際時(shí)間少1天,也可能是小時(shí)少了13-14小時(shí)
Mysql的時(shí)區(qū)是CST(使用語(yǔ)句:show VARIABLES LIKE ’%time_zone%’; 查)
先放總結(jié):
修改方法:
1. 修改數(shù)據(jù)庫(kù)時(shí)區(qū)
2. 在jdbc.url里加后綴 &serverTimezone=GMT%2B8
3. 代碼里設(shè)置時(shí)區(qū),給SimpleDateFormat.setTimeZone(...)
例外:new Date() 可以直接存為正確時(shí)間,其他的不行。比如我試過(guò),把new Date用sdf轉(zhuǎn)個(gè)2次,然后就錯(cuò)誤了
貼一下測(cè)試的一下渣碼
// 1.new Date()直接存數(shù)據(jù)庫(kù)則是正確的日期 結(jié)果:√ 190626,數(shù)據(jù)庫(kù)存儲(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è)置中國(guó)標(biāo)準(zhǔn)時(shí)區(qū) UTC+8 結(jié)果:√// SimpleDateFormat sdf = new SimpleDateFormat('yyMMdd'); // 設(shè)置時(shí)區(qū): 中國(guó)標(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é)果:× 問(wèn)題就在于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類(lèi)型的時(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ù)庫(kù)里仍然用Date,用數(shù)據(jù)庫(kù)的時(shí)間函數(shù)轉(zhuǎn)化
最后,還是采用的數(shù)據(jù)庫(kù)為timestamp類(lèi)型,用mysql的時(shí)間函數(shù)進(jìn)行轉(zhuǎn)換,保證時(shí)間為數(shù)據(jù)庫(kù)時(shí)間
補(bǔ)充知識(shí):mybatis解決java中的date類(lèi)型存入oracle數(shù)據(jù)庫(kù)之后不顯示時(shí)分秒
實(shí)體類(lèi)中類(lèi)型為java.util.Date
private Date update_date;
數(shù)據(jù)庫(kù)中對(duì)應(yīng)字段的類(lèi)型為Date
不顯示 時(shí)分秒 的情況:
Mapping文件中對(duì)應(yīng)字段的jdbcType為DATE類(lèi)型
如果顯示時(shí)分秒的話,只需要將Mapping文件中對(duì)應(yīng)字段的類(lèi)型改為T(mén)IMESTAMP即可.
以上這篇淺談Mybatis+mysql 存儲(chǔ)Date類(lèi)型的坑就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 快速解決mysql導(dǎo)出scv文件亂碼、躥行的問(wèn)題2. Mysql InnoDB和MyISAM區(qū)別原理解析3. SqlServer 多種分頁(yè)方式 詳解(含簡(jiǎn)單速度測(cè)試)4. navicat for mysql導(dǎo)出數(shù)據(jù)庫(kù)的方法5. MySql使用mysqldump 導(dǎo)入與導(dǎo)出方法總結(jié)6. MySQL 數(shù)據(jù)丟失排查案例7. MySQL5.7 mysqldump備份與恢復(fù)的實(shí)現(xiàn)8. Mybatis傳入List實(shí)現(xiàn)批量更新的示例代碼9. SQLite3 命令行操作指南10. Microsoft Office Access隱藏和顯示字段的方法
