Java Sorting Date field

后端 未结 1 1175
旧巷少年郎
旧巷少年郎 2021-01-26 02:17

I have a date like String date=\"June 02,2013\" etc.....

I want to Sort the Date field in Ascending and Descending

Could any one help?

I ha

相关标签:
1条回答
  • 2021-01-26 03:18

    Try this...

    Collections.sort(datestring, new Comparator<String>() {
        DateFormat f = new SimpleDateFormat("MMM dd,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);
            }
        }
    });
    

    you need to change format of date in the above code.

    UPDATED : Updated DateFormat check it..

    0 讨论(0)
提交回复
热议问题