0

So, I have a list of dates that is something like this:

List<String> dates = ["2012-05-16", "2012-05-18", "2012-06-19", "2013-01-18", "2013-01-10", "2013-08-05", "2010-07-10"...]

The list goes on with like 100 dates and I want to retrieve all the years that exists on the list. (to get a result like this)

List<String> years = ["2012", "2013", "2010"...]
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110

4 Answers4

3

Use a substring to split the first 4 characters of each item out. It will leave you with the year

   for (String s : dates) {
         String year = s.subString(0,5);
         years.add(year);

      }

or you could use the .split() method

String year =  s.split("-")[0];

EDIT

Upon further clarification, the question was to get unique years in the array list.

This can be done by passing the arraylist into a Set which does not accept duplicate values and then passing the set back into the array

// add elements to al, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(years);
years.clear();
years.addAll(hs);

From How do I remove repeated elements from ArrayList?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Isaac
  • 310
  • 5
  • 16
0

you can check for duplicates before adding into a list (or you can use a Set instead of a List).

   for (String date : dates) {
         String year = date.substring(0,5);
         if(!years.contain(year)){
             years.add(year); 
         }else{
             //already in the list
         }
    }
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
0
List<String> years = dates.stream()
    .map(x -> x.substring(0, 4))
    .distinct()
    .collect(Collectors.toList());
  • For each entry you take just the first 4 characters (you could even parse the string into a date and then you just get the year)
  • After that you take each value distinctly (no duplicates)
  • And you collect that stream into a List
Davide
  • 1,931
  • 2
  • 19
  • 39
0

java.time

I recommend you do it using the modern Date-Time API. Depending on your requirement, you can get the list of years in the form of Integer, String, Year etc.

Demo:

import java.time.LocalDate;
import java.time.Year;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        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)
                    .map(d -> String.valueOf(d.getYear()))
                    .collect(Collectors.toList());

        List<Year> yearsYearList = 
                dates.stream()
                    .map(LocalDate::parse)
                    .map(d -> Year.of(d.getYear()))
                    .collect(Collectors.toList());

        System.out.println(yearsIntList);
        System.out.println(yearsStrList);
        System.out.println(yearsYearList);
    }
}

Output:

[2012, 2012, 2012, 2013, 2013, 2013, 2010]
[2012, 2012, 2012, 2013, 2013, 2013, 2010]
[2012, 2012, 2012, 2013, 2013, 2013, 2010]

ONLINE DEMO

Note that all of your date strings are in the ISO 8601 format. The modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.

Learn more about the modern Date-Time API* from Trail: Date Time.

Note:

Use .distinct() before collecting the Stream if you need a unique collection of years e.g.

List<String> yearsStrList = 
    dates.stream()
        .map(LocalDate::parse)
        .map(d -> String.valueOf(d.getYear()))
        .distinct()
        .collect(Collectors.toList());

Output:

[2012, 2013, 2010]

ONLINE DEMO


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110