I'm using the Java 8 time library to determine the previous weeks and year from today's date and then store those values in a map. I think I've figured out how to properly get the weeks, but I'm not entirely sure what the correct way is to get the year. Current code below:
int weeksLimit = 5; // this value is passed into the method, but just setting it here as an example
Map<Integer, Integer> yearWeeks = new HashMap<Integer, Integer>();
LocalDate ld = LocalDate.now();
WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 4);
for (int i = weeksLimit; i > 0; i--) {
LocalDate pastDate = ld.minusWeeks(i);
yearWeeks.put(pastDate.getYear(), pastDate.get(weekFields.weekOfWeekBasedYear()));
}
I don't believe pastDate.getYear()
is the correct usage. As the result I want is something along the lines of:
- 1/12/2020 - 1/18/2020 = Week 3, Year 2020
- 1/05/2020 - 1/11/2020 = Week 2, Year 2020
- 12/29/2019 - 1/04/2020 = Week 1, Year 2020
- 12/22/2019 - 12/28/2019 = Week 52, Year 2019
Could someone please help me figure out how to correctly get the year I want?