Java 获取三天前时间

2022/8/25 14:23:04

本文主要是介绍Java 获取三天前时间,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

(1)根据new Date().getTime()获取从1970年1月1日0点0分到目前的毫秒数计算三天前的时间:

   Date dateBy3Days = new Date(new Date().getTime()-3*24*60*60*1000);
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   String threeDayAgo = sdf.format(dateBy3Days);
   threeDayAgo = threeDayAgo + "00:00:00";

 

(2)Java 1.8后:

  String threeDayAgo = LocalDateTime.now().plusDays(-3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));



这篇关于Java 获取三天前时间的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程