2

I have 2 lists which contains exact similar dates but the formatting is different. How can I compare those 2 lists are equal ignoring the format of the date?

Eg:

List 1:

[2016-07-22, 2016-07-20, 2016-07-18, 2016-07-18, 2016-07-18, 2016-07-20, 2016-07-18]

List 2:

[22.07.2016, 20.07.2016, 18.07.2016, 18.07.2016, 18.07.2016, 20.07.2016, 18.07.2016]

Let me know if there is anyway I can do this.

4
  • 3
    Parse the dates in both lists so that you have two lists with actual dates. Then compare those two lists. Commented Aug 26, 2016 at 13:55
  • You have List<String> right ? Commented Aug 26, 2016 at 13:59
  • Yes I have List<String>. Commented Aug 26, 2016 at 14:00
  • @RV_Dev check my answer Commented Aug 26, 2016 at 14:28

2 Answers 2

1

Updated:

You can use this logic to compare two date.

    SimpleDateFormat sf1= new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sf2= new SimpleDateFormat("dd.MM.yyyy");
    Date date1=sf1.parse("2016-07-18");
    Date date2=sf2.parse("18.07.2016");
    boolean bol=date1.compareTo(date2)==0;
    System.out.println(bol);//true if two dates are equal, false if two date are not equal

Example

    Date[] dl1={sf1.parse("2016-07-22"), sf1.parse("2016-07-20"), sf1.parse("2016-07-18"), sf1.parse("2016-07-18"), sf1.parse("2016-07-18"), sf1.parse("2016-07-20"), sf1.parse("2016-07-18")};
    Date[] dl2={sf2.parse("22.07.2016"), sf2.parse("20.07.2016"), sf2.parse("18.07.2016"), sf2.parse("18.07.2016"), sf2.parse("18.07.2016"), sf2.parse("20.07.2016"), sf2.parse("18.07.2016")};

    List<Date> l1= Arrays.asList(dl1);
    List<Date> l2= Arrays.asList(dl2);



    for(int i=0;i<l1.size();i++){
        if(l1.get(i).compareTo(l2.get(i))==0){
            System.out.println("Date matched");
        }else{
            System.out.println("Date did not matched");
        }
    }

Output:

Date matched
Date matched
Date matched
Date matched
Date matched
Date matched
Date matched
Sign up to request clarification or add additional context in comments.

Comments

1

Parse the dates:

List<String> list1 = // ...
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
List<LocalDate> dates1 = list1.stream()
                              .map(s -> LocalDate.parse(s, formatter1))
                              .collect(Collectors.toList());

// same with list2 and an adapted formatter
List<String> list2 = // ...
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd.MM.yyyy");
List<LocalDate> dates2 = list2.stream()
                              .map(s -> LocalDate.parse(s, formatter2))
                              .collect(Collectors.toList());

boolean equal = dates1.equals(dates2);

4 Comments

-> what is that operator called? I didn't see that before in java.
It's a Java8 expression. Lambda.
@Jean Logeart : java.time.format.DateTimeParseException: Text '22.07.2016' could not be parsed at index 0.
I did the same thing still getting same error. Shouldn't this be as .collect(Collector.toList());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.