If the acquisition target is the past 3 months This Week: Get the dates of the Monday and Sunday of the week that contains the current date Past Weeks: Get dates for each Monday and Sunday Last week: Get dates for Mondays and Sundays of the week containing dates 3 months ago
I received an indication from swordone and corrected the logic (6/25). * See comments
public static void main(String[] args) {
//Get current date
LocalDate now = LocalDate.now();
//Get the date 3 months ago (change this date as appropriate, 3 months in this example)
LocalDate targetDate = now.minusMonths(3);
//Get the Monday of the current week
LocalDate monday = now.with(DayOfWeek.MONDAY);
//Get the Sunday of the current week
LocalDate sunday = now.with(DayOfWeek.SUNDAY);
int i = 0;
while (!sunday.isBefore(targetDate)) {
System.out.println(monday + " ~ " + sunday + " : " + (i == 0 ? "this week" : i + "Week ago"));
monday = monday.minusDays(7);
sunday = sunday.minusDays(7);
i++;
}
}
The current date is 6/25. The acquisition target is up to 3 months in advance. I think it can be used when you want to specify the period for the past few weeks. Change the date to be acquired as appropriate and use it.
2019-06-24 ~ 2019-06-30 :this week
2019-06-17 ~ 2019-06-23 :1 week ago
2019-06-10 ~ 2019-06-16 :2 weeks ago
2019-06-03 ~ 2019-06-09 :3 weeks ago
2019-05-27 ~ 2019-06-02 :4 weeks ago
2019-05-20 ~ 2019-05-26 :5 weeks ago
2019-05-13 ~ 2019-05-19 :6 weeks ago
2019-05-06 ~ 2019-05-12 :7 weeks ago
2019-04-29 ~ 2019-05-05 :8 weeks ago
2019-04-22 ~ 2019-04-28 :9 weeks ago
2019-04-15 ~ 2019-04-21 :10 weeks ago
2019-04-08 ~ 2019-04-14 :11 weeks ago
2019-04-01 ~ 2019-04-07 :12 weeks ago
2019-03-25 ~ 2019-03-31 :13 weeks ago
Recommended Posts