Was java.time.LocalDate last week?

I have a weekly statistics system and I want to check if the latest statistics were updated last week. If that were the case, I want to clean it.

Currently, the date is stored in LocalDate:

LocalDate lastUpdate;

I already have daily statistics working as follows:

if(lastUpdate.isBefore(LocalDate.now())
{
    clearDailyStatistics();
}

And I would like to do something like this:

if(/* something here? */)
{
    clearWeeklyStatistics();
}

So the question is: how to compare weeks of objects LocalDate? There is no need for different "standards" of how the week is formed - from Monday to Sunday (as it is already used by the Java class DayOfWeek).

Please leave your comments and answers only about Java 8 java.time classes, no Joda-Time or other external libraries.

+4
2

, 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))
+6

7 1

if(lastUpdate.plusDays(7).isBefore(LocalDate.now())
{
    clearWeeklyStatistics();
}

if(lastUpdate.plusWeeks(1).isBefore(LocalDate.now()){

  clearWeeklyStatistics();

  }

: - , 7 . . .

0

Source: https://habr.com/ru/post/1544872/


All Articles