import java.time.LocalDate; import java.time.Year; import java.util.List; import java.util.stream.Collectors; public class Main { List<String> dates = List.of("2012-05-16", "2012-05-18", "2012-06-19", "2013-01-18", "2013-01-10", "2013-08-05", "2010-07-10"); List<Integer> yearsIntList = dates.stream() .map(LocalDate::parse).map(d -> d.getYear()) .collect(Collectors.toList()); List<String> yearsStrList = dates.stream() .map(LocalDate::parse) .collect(Collectors.toList()); List<Year> yearsYearList = dates.stream() .map(LocalDate::parse) .map(d -> Year.of(d.getYear())) .collect(Collectors.toList()); } }
Standard input is empty
[2012, 2012, 2012, 2013, 2013, 2013, 2010] [2012, 2012, 2012, 2013, 2013, 2013, 2010] [2012, 2012, 2012, 2013, 2013, 2013, 2010]