-12

I have array list of dates-

24/04/2019,

13/05/2019,

12/04/2019,

11/04/2019

And if say today's date is 11/04/2019 then I want to show an output or result like this-

11/04/2019,

12/04/2019,

24/04/2019,

13/05/2019

Also, I am use collection. sort but how I use for this array list by using the current date.

Michele La Ferla
  • 6,535
  • 11
  • 48
  • 77
  • You can use `Collections.sort()`. – Prasad Karunagoda Jan 24 '19 at 13:11
  • Please have a look into: https://stackoverflow.com/questions/5927109/sort-objects-in-arraylist-by-date if still you feel difficulty, let us know. – Chang Jan 28 '19 at 20:38
  • How do you want to use current date? The example you show just shows chronological ordering, current day doesn’t seem to be handled in any special way? Your question is very unclear. – Ole V.V. Feb 16 '19 at 08:02

1 Answers1

0

The method is for sorting dates in ascending order.

You can go with Comparator and can sort data by using compare()

Collections.sort(dateList, new Comparator<Date>(){
           public int compare(Date date1, Date date2){
          return date1.after(date2);
        }
      });

dateList is list of Dates ArrayList<Date>.

If your dates are in string format then convert them to Date format.

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
  Date d = sdf.parse("11/04/2019");
} catch (ParseException ex) {
  Log.v("Exception", ex.getLocalizedMessage());
}
Jitesh Prajapati
  • 2,661
  • 4
  • 28
  • 47
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Feb 16 '19 at 07:59