我知道我可以使用new Date()获取当前日期,但我想获取过去的日期(12周前)。例如:今天是2014年1月3日,12周前的日期是2013年10月11日。
如何在java中做到这一点?
发布于 2014-01-03 11:04:19
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)发布于 2014-01-03 11:06:37
大多数Date实际上已被弃用,它已被Calendar所取代
Calendar.getInstance().add(Calendar.WEEK_OF_YEAR, -12);发布于 2014-01-03 11:16:35
使用JDK1.8的LocalDate.minusWeeks,
LocalDate first=LocalDate.now();
LocalDate second=first.minusWeeks(12);https://stackoverflow.com/questions/20895784
复制相似问题