Java集合按时间排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mrlin6688/article/details/81675551
 /**
     * 根据时间排序
     * @param list
     */
    private static void ListSort(List<BillHistoryEntity> list) {
        Collections.sort(list, new Comparator<BillHistoryEntity>() {
            @Override
            public int compare(BillHistoryEntity o1, BillHistoryEntity o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date dt1 = format.parse(o1.getSubmitTime());
                    Date dt2 = format.parse(o2.getSubmitTime());
                    if (dt1.getTime() > dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }

猜你喜欢

转载自blog.csdn.net/mrlin6688/article/details/81675551
今日推荐