兩者關(guān)系
sql包中Date是util包子類
public class Date extends java.util.Date
兩者在時(shí)間顯示上區(qū)別
/**
* 解決sql包中日期和util包日期轉(zhuǎn)換問題
*/
@Test
public 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類輸出的時(shí)間包含年月日及時(shí)分秒,而java.sql.Date輸出的時(shí)間僅有年月日。這是因?yàn)閖ava.sql包下的Date僅表示日期,只有年月日,沒有時(shí)分秒,因此會丟失時(shí)間。文章來源:http://www.zghlxwxcb.cn/news/detail-638990.html
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]文章來源地址http://www.zghlxwxcb.cn/news/detail-638990.html
到了這里,關(guān)于java.sql.Date & java.util.Date的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!