fork download
  1. import java.time.YearMonth;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.List;
  4. import java.util.Locale;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.Stream;
  7.  
  8. public class Main {
  9. public static void main(String args[]){
  10. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM uuuu", Locale.ENGLISH);
  11. List<String> sorted = Stream.of(
  12. "Sep 2019",
  13. "Oct 2018",
  14. "Nov 2019",
  15. "Oct 2021",
  16. "Nov 2011"
  17. )
  18. .map(s -> YearMonth.parse(s, dtf))
  19. .sorted()
  20. .map(ym -> dtf.format(ym))
  21. .collect(Collectors.toList());
  22.  
  23. // Display the list
  24. sorted.forEach(System.out::println);
  25. }
  26. }
Success #stdin #stdout 0.12s 52628KB
stdin
Standard input is empty
stdout
Nov 2011
Oct 2018
Sep 2019
Nov 2019
Oct 2021