两者关系
sql包中Date是util包子类
public class Date extends java.util.Date
两者在时间显示上区别
/**
* 解决sql包中日期和util包日期转换问题
*/
@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类输出的时间包含年月日及时分秒,而java.sql.Date输出的时间仅有年月日。这是因为java.sql包下的Date仅表示日期,只有年月日,没有时分秒,因此会丢失时间。文章来源:https://www.toymoban.com/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]文章来源地址https://www.toymoban.com/news/detail-638990.html
到了这里,关于java.sql.Date & java.util.Date的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!