文章目录
两者关系
sql包中Date是util包子类
java
public class Date extends java.util.Date
两者在时间显示上区别
java
/**
* 解决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仅表示日期,只有年月日,没有时分秒,因此会丢失时间。
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]