6

我需要用户定义的函数,它可以将日期从当前时间排序到旧时间。

我有 10 个日期的列表,我想从最近的日期开始对这些日期进行排序。

目前我有一个逻辑,如果我们可以以毫秒为单位转换日期,那么将它与当前毫秒进行比较,最小毫秒将是最近的日期。那是,

CURRENT_MILLI_SECOND - A_DATE_CONVERTED_TO_MILLI_SECONDS = MILLI-SECONDS

如果有人可以在这个逻辑或任何其他逻辑上帮助我,请建议我......!!!

这是我从服务器获得的甲酸盐:

Thu Dec 27 11:02:43 GMT+05:30 2012
4

7 回答 7

7

您可以使用Comparator并可以使用compare()对数据进行排序

Collections.sort(dateList, new Comparator<Date>(){
           public int compare(Date date1, Date date2){
          return date1.after(date2);
        }
      });
于 2012-12-27T05:53:09.513 回答
5

试试这个:

 Comparator date_comparator = new Comparator() {
    @Override
    public int compare(Date date1, Date date2){
    return date1.compareTo(date2);
    }
    };
于 2012-12-27T06:08:12.217 回答
1

你可以使用CalendarorDate类来做到这一点。使用日历和日期,您可以比较两个日期,例如date1.compare(date2)date.before(date2)date1.after(date2)此类 API 可用于您的案例。

于 2012-12-27T05:55:45.970 回答
1
于 2018-01-14T01:13:34.540 回答
0

Java 中的DateCalendar类已经有一个很好的 API 来比较日期。

您可以使用它来编写自己的函数来对日期进行排序。

你可以参考这个链接

于 2012-12-27T06:00:22.383 回答
0

有我的要点可以帮助你。您只需要在SortedDateGenerator.getSortedDates(List<String> unSortedDate,String datePattern) 函数的参数中传递未排序的日期字符串和日期模式,然后您将获得排序日期的字符串。如果您需要Date对象,那么您可以根据您的要求修改我的代码。

于 2019-12-18T06:57:04.173 回答
0

只需调用getSortedDateList方法即可根据您的要求对日期进行排序

private ArrayList<Date> getSortedDateList(ArrayList<Date> dateList) {
        //Replace year with current year to sort in jan to dec order
        for(int i = 0; i < dateList.size(); i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateList.get(i));
            calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
            dateList.set(i, calendar.getTime());
        }
        //Sort all dates in ascending order
        Collections.sort(dateList);
        //Get final index of past dates
        int index = 0;
        for (int i = 0; i < dateList.size(); i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateList.get(i));
            calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
            Calendar today = Calendar.getInstance();
            today.set(Calendar.DATE, Calendar.getInstance().get(Calendar.DATE) - 1);
            if (calendar.getTime().after(today.getTime())) {
                index = i;
                break;
            }
        }
        //New list for storing upcoming dates
        ArrayList<Date> newList = new ArrayList<>();
        
        //Store upcoming dates in new list
        for (int i = index; i < dateList.size(); i++) {
            newList.add(dateList.get(i));
        }
        //Store past dates in new list
        for (int i = 0; i < index; i++) {
            newList.add(dateList.get(i));
        }
        Collections.copy(dateList, newList);
        return dateList;
    }

例如,将一些示例日期存储到数组列表并调用getSortedDateList方法

SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy"); // Your date format
        
ArrayList<Date> dateList = new ArrayList<>();
dateList.add(formatter.parse("Thu May 25 00:00:00 GMT+05:30 2000"));
dateList.add(formatter.parse("Tue Apr 18 00:00:00 GMT+05:30 2021"));
dateList.add(formatter.parse("Wed Jan 27 00:00:00 GMT+05:30 1997"));
dateList.add(formatter.parse("Sat Feb 22 00:00:00 GMT+05:30 2020"));
dateList.add(formatter.parse("Mon Dec 20 00:00:00 GMT+05:30 2001"));
dateList.add(formatter.parse("Thu Jun 14 00:00:00 GMT+05:30 2009"));
dateList.add(formatter.parse("Sat Mar 01 00:00:00 GMT+05:30 1999"));
dateList.add(formatter.parse("Sat Nov 07 00:00:00 GMT+05:30 2005"));
        
dateList = getSortedDateList(dateList);
        
for(Date date: dateList) {
    System.out.println(date.toString());
}

如果当前日期是Feb 01 2021,那么最终输出将如下所示

Mon Feb 22 00:00:00 IST 2021
Mon Mar 01 00:00:00 IST 2021
Sun Apr 18 00:00:00 IST 2021
Tue May 25 00:00:00 IST 2021
Mon Jun 14 00:00:00 IST 2021
Sun Nov 07 00:00:00 IST 2021
Mon Dec 20 00:00:00 IST 2021
Wed Jan 27 00:00:00 IST 2021
于 2021-02-01T10:01:48.410 回答