Java : Sort Date Object and Text Format

In Java classes java.util.Date, java.util.Calendar, java.time.LocalDateTime, java.time.LocalDate and java.time.LocalTime  implements comparable interface for natural ordering. We can sort these objects implements comparable interface as below


Collections.sort(List <Date> list);
Collections.sort(List <Calendar> list);
Collections.sort(List <LocalDateTime> list);
Collections.sort(List <LocalDate> list);
Collections.sort(List <LocalTime> list);

Problem occurred when date in String format and following different format pattern as dd/MM/yyyy or some more patterns then you have to implement Comparator to sort this date text patterns as below


Collections.sort(dateStrList, new Comparator() {
DateFormat f = new SimpleDateFormat("dd/MM/yyyy"); 

@Override public int compare(String o1, String o2) {
 try { 
   return f.parse(o1).compareTo(f.parse(o2)); 
} catch (ParseException e) { 
throw new IllegalArgumentException(e); 
} 
} }); 

To learn Comparator and Comparable check below link.

Java : Comparable Vs Comparator

In below example you will see sorting of date  in text format and objects also.

Example

package com.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortingDate {

public static void main(String[] args) {
 //java 8
sortDateObject();
sortDateText();
}

private static void sortDateText() {
List<String>dateStrList=new ArrayList<String>();
// Date format dd/MM/YYYY
dateStrList.add("03/04/2016");
dateStrList.add("03/01/2016");
dateStrList.add("02/06/2016");
dateStrList.add("02/09/2016");
dateStrList.add("22/09/2016");
dateStrList.add("16/09/2016");
dateStrList.add("03/04/2016");
dateStrList.add("01/08/2017");
dateStrList.add("02/06/2017");
dateStrList.add("02/09/2014");
dateStrList.add("22/09/2018");
dateStrList.add("16/09/2015");

System.out.println("---> Date List Before Sort (dd/MM/yyyy)");
for(String dateStr:dateStrList)
System.out.println(dateStr);

//Sort String Date
Collections.sort(dateStrList, new Comparator<String>() {
DateFormat f = new SimpleDateFormat("dd/MM/yyyy");
    @Override
    public int compare(String o1, String o2) {
    try {
	 return f.parse(o1).compareTo(f.parse(o2));
	} catch (ParseException e) {
	       throw new IllegalArgumentException(e);
	   }
	   }
	});

    System.out.println("---> Date List After Sort (dd/MM/yyyy)");
    for(String dateStr:dateStrList)
    System.out.println(dateStr);

	}

private static void sortDateObject() {
List<LocalDate>dateList=new ArrayList<LocalDate>();
dateList.add(LocalDate.of(2012, 05, 12));
dateList.add(LocalDate.of(2014, 03, 23));
dateList.add(LocalDate.of(2011, 02, 13));
dateList.add(LocalDate.of(2013, 11, 12));
dateList.add(LocalDate.of(2017, 8, 11));
dateList.add(LocalDate.of(2016, 9, 05));

DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofPattern("MM/dd/yyyy");
System.out.println("---> Date  Object List Before Sort (MM/dd/yyyy)");
for(LocalDate localDate:dateList)
{
  System.out.println(localDate.format(dateTimeFormatter));
}
// LocalDate  internally having Comparable interface no need additional Comparator
Collections.sort(dateList);

System.out.println("---> Date Object List After Sort (MM/dd/yyyy)");
for(LocalDate localDate:dateList)
{
System.out.println(localDate.format(dateTimeFormatter));
}

}

}

Output


---> Date  Object List Before Sort (MM/dd/yyyy)
05/12/2012
03/23/2014
02/13/2011
11/12/2013
08/11/2017
09/05/2016
---> Date Object List After Sort (MM/dd/yyyy)
02/13/2011
05/12/2012
11/12/2013
03/23/2014
09/05/2016
08/11/2017
---> Date List Before Sort (dd/MM/yyyy)
03/04/2016
03/01/2016
02/06/2016
02/09/2016
22/09/2016
16/09/2016
03/04/2016
01/08/2017
02/06/2017
02/09/2014
22/09/2018
16/09/2015
---> Date List After Sort (dd/MM/yyyy)
02/09/2014
16/09/2015
03/01/2016
03/04/2016
03/04/2016
02/06/2016
02/09/2016
16/09/2016
22/09/2016
02/06/2017
01/08/2017
22/09/2018

Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.