fork(1) download
  1. import java.time.format.DateTimeFormatter;
  2. import java.time.YearMonth;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. class Main {
  7. public static void main(String[] args) {
  8. Map<String, Long> expectedValues = new HashMap<>();
  9. expectedValues.put("2021-06-01", 10L);
  10. expectedValues.put("2021-07-01", 20L);
  11. expectedValues.put("2021-08-01", 30L);
  12.  
  13. transform(expectedValues);
  14.  
  15. // Show the map, sorted by date
  16. expectedValues.entrySet().stream()
  17. .sorted(Map.Entry.comparingByKey())
  18. .forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
  19. }
  20.  
  21. static void transform(Map<String, Long> expectedValues) {
  22. YearMonth thisMonth = YearMonth.now();
  23. DateTimeFormatter format = DateTimeFormatter.ISO_LOCAL_DATE;
  24. for (int i = 0; i < 12; i++) {
  25. String prev = thisMonth.minusMonths(i).atDay(1).format(format);
  26. if (!expectedValues.containsKey(prev)) {
  27. expectedValues.put(prev, 0L);
  28. }
  29. }
  30. }
  31. }
Success #stdin #stdout 0.15s 51656KB
stdin
Standard input is empty
stdout
2020-10-01: 0
2020-11-01: 0
2020-12-01: 0
2021-01-01: 0
2021-02-01: 0
2021-03-01: 0
2021-04-01: 0
2021-05-01: 0
2021-06-01: 10
2021-07-01: 20
2021-08-01: 30
2021-09-01: 0