我知道我可以使用 new Date()
获取当前日期,但我想获取过去的日期(12 周前)。例如:今天是 2014 年 1 月 3 日,12 周前的日期将为 2013 年 10 月 11 日。
如何在java中做到这一点?
最佳答案
Date date = new Date();
long timeAgo = 1000L * 60 * 60 * 24 * 7 * 12;
Date dateAgo = new Date(date.getTime() - timeAgo);
应该可以正常工作。不要错过乘法中的L
,否则你会得到溢出结果。好地方 Pshemo .
仅供引用,timeAgo 列如下:
1000 is representative of a second (1000 milliseconds)
1000 * 60 is representative of a minute (60 seconds)
1000 * 60 * 60 is representative of an hour (60 minutes)
1000 * 60 * 60 * 24 is representative of a day (24 hours)
1000 * 60 * 60 * 24 * 7 is representative of a week (7 days)
1000 * 60 * 60 * 24 * 7 * 12 is representative of 12 weeks (12 weeks)
关于java - java中如何计算12周前的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20895784/