fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.LocalDate;
  7. import java.time.YearMonth;
  8. import java.time.format.DateTimeFormatter;
  9. import java.util.List;
  10. import java.util.stream.Collectors;
  11. import java.util.stream.Stream;
  12.  
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone
  16. {
  17. public static void main(String[] args) {
  18. String strings = "04/11/2019, 11/11/2019, 18/11/2019, 25/11/2019, 02/12/2019, 09/12/2019, 06/01/2020, 03/02/2020, 10/02/2020, 17/02/2020, 24/02/2020, 02/03/2020, 09/03/2020, 16/03/2020, 23/03/2020, 30/03/2020, 06/04/2020, 13/04/2020, 20/04/2020, 27/04/2020";
  19. DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/uuuu");
  20. List<LocalDate> collect = Stream.of(strings.split(", "))
  21. .map(s -> LocalDate.parse(s, format))
  22. .collect(Collectors.groupingBy(YearMonth::from))
  23. .values().stream()
  24. .map(a -> a.stream().sorted().limit(1).findFirst().get())
  25. .sorted()
  26. .collect(Collectors.toList());
  27.  
  28. System.out.println(collect);
  29. }
  30. }
Success #stdin #stdout 0.11s 36000KB
stdin
Standard input is empty
stdout
[2019-11-04, 2019-12-02, 2020-01-06, 2020-02-03, 2020-03-02, 2020-04-06]