[英]Was a java.time.LocalDate in last week?
I have a weekly statistics system, and I want to check if the last time the statistic was updated was last week. 我有一个每周统计系统,我想检查统计数据最后一次更新是否是上周。 If it was, I want to clear it. 如果是的话,我想清除它。
At the moment, the date is stored in a LocalDate
: 目前,日期存储在LocalDate
:
LocalDate lastUpdate;
I already have the daily statistic working like this: 我已经有了这样的日常统计数据:
if(lastUpdate.isBefore(LocalDate.now())
{
clearDailyStatistics();
}
And I'd like to do something like this: 我想做这样的事情:
if(/* something here? */)
{
clearWeeklyStatistics();
}
So, the question is: How do I compare the weeks of LocalDate
objects? 所以,问题是: 我如何比较LocalDate
对象的周数? There is no need for different "standards" of how week is formed - Monday through Sunday will be used (as is already used by Java's DayOfWeek
class). 没有必要使用不同的“标准”来形成星期 - 星期一到星期日将被使用(正如Java的DayOfWeek
类已经使用的那样)。
Please, keep your comments and answers only about Java 8 java.time classes, no Joda-Time or other external libraries. 请保留您关于Java 8 java.time类,没有Joda-Time或其他外部库的注释和答案。
Try comparing the year and the week in the year, using WEEK_OF_WEEK_BASED_YEAR : 尝试使用WEEK_OF_WEEK_BASED_YEAR比较一年中的年份和周数:
LocalDate now = LocalDate.now();
if(lastUpdate.get(IsoFields.WEEK_BASED_YEAR) != now.get(IsoFields.WEEK_BASED_YEAR)
|| lastUpdate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR) !=
now.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR))
Add 7 days or 1 week to your date and compare with current date 将7天或1周添加到您的日期并与当前日期进行比较
if(lastUpdate.plusDays(7).isBefore(LocalDate.now())
{
clearWeeklyStatistics();
}
OR 要么
if(lastUpdate.plusWeeks(1).isBefore(LocalDate.now()){
clearWeeklyStatistics();
}
Edit:- Consider this solution only if you want to compare the crossing of week in terms of 7 days period , See the comments for more details. 编辑: - 仅当您想要比较7天的周期交叉时才考虑此解决方案。有关详细信息,请参阅注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.