Exact error: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
Below is a HashMap I've created where we can see only 3 months of data are present. The end goal is to create/update the MAP dynamically with last 12 moths of data, from current month.
Any month other than these 3 months will have value as 0L.
Map<String, Long> expectedValues = new HashMap<>();
expectedValues.put("2021-06-01", 10L);
expectedValues.put("2021-07-01", 20L);
expectedValues.put("2021-08-01", 30L);
I have written this logic to check and populate 0L for non-existing months. However getting the error while using the DateTimeFormatter
YearMonth thisMonth = YearMonth.now();
for (int i = 0; i < 12; i++) {
// DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter monthYearFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String prevYearMonthStr = thisMonth.minusMonths(i).format(monthYearFormatter);
if (!expectedValues.containsKey(prevYearMonthStr)) {
expectedValues.put(prevYearMonthStr, 0L);
}
}
How can we fix this error.