需求

从数据库查询出来的String类型的时间,要和当前时间计算差值,得到相差几天

思路

将数据库查询出来的字符串实现转为date 类型,获取到当前时间的date类型
获取两个时间的毫秒值,作差,最后计算天数

代码实现

public class MyTest {
   

    @SneakyThrows
    public static void main(String[] args) {
   
  获取到当前的时间的date类型
        Date now = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("YYYY-MM-dd");
将数据库时间转为date类型
        Date parse = ft.parse("2020-08-05");
获取到时间的毫秒值
		long nowtime = now.getTime();
		long time = parse.getTime();
毫秒值作差
        long cz = nowtime - time;
计算天数
        String dqsj = ""+Math.abs(cz) / 1000 / 60 / 60 / 24 ;


// System.out.println(ft.format(now) instanceof String);
// System.out.println(nowtime);
        System.out.println(dqsj);
    }
}