fork download
  1. import java.time.LocalDate;
  2. import java.time.Year;
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8. List<String> dates = List.of("2012-05-16", "2012-05-18", "2012-06-19", "2013-01-18", "2013-01-10", "2013-08-05",
  9. "2010-07-10");
  10.  
  11. List<Integer> yearsIntList =
  12. dates.stream()
  13. .map(LocalDate::parse).map(d -> d.getYear())
  14. .collect(Collectors.toList());
  15.  
  16. List<String> yearsStrList =
  17. dates.stream()
  18. .map(LocalDate::parse)
  19. .map(d -> String.valueOf(d.getYear()))
  20. .collect(Collectors.toList());
  21.  
  22. List<Year> yearsYearList =
  23. dates.stream()
  24. .map(LocalDate::parse)
  25. .map(d -> Year.of(d.getYear()))
  26. .collect(Collectors.toList());
  27.  
  28. System.out.println(yearsIntList);
  29. System.out.println(yearsStrList);
  30. System.out.println(yearsYearList);
  31. }
  32. }
Success #stdin #stdout 0.11s 49644KB
stdin
Standard input is empty
stdout
[2012, 2012, 2012, 2013, 2013, 2013, 2010]
[2012, 2012, 2012, 2013, 2013, 2013, 2010]
[2012, 2012, 2012, 2013, 2013, 2013, 2010]