鮮花網(wǎng)站建設(shè)主要內(nèi)容it培訓(xùn)機(jī)構(gòu)排行榜
文章目錄
- 兩者關(guān)系
- 兩者在時(shí)間顯示上區(qū)別
兩者關(guān)系
sql包中Date是util包子類(lèi)
public class Date extends java.util.Date
兩者在時(shí)間顯示上區(qū)別
/*** 解決sql包中日期和util包日期轉(zhuǎn)換問(wèn)題*/@Testpublic void t3(){Date utilDate = new Date();java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());Time time = new Time(utilDate.getTime());Timestamp timestamp = new Timestamp(utilDate.getTime());log.info("[{}]",utilDate);log.info("[{}]",sqlDate);log.info("[{}]",time);log.info("[{}]",timestamp);}
我們可以看到,java.util.Date類(lèi)輸出的時(shí)間包含年月日及時(shí)分秒,而java.sql.Date輸出的時(shí)間僅有年月日。這是因?yàn)閖ava.sql包下的Date僅表示日期,只有年月日,沒(méi)有時(shí)分秒,因此會(huì)丟失時(shí)間。
21:59:40.745 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - [Sun Aug 06 21:59:40 CST 2023]
21:59:40.762 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - [2023-08-06]
21:59:40.762 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - [21:59:40]
21:59:40.762 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - [2023-08-06 21:59:40.736]