Java排序日期字段 [英] Java Sorting Date field

查看:92
本文介绍了Java排序日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期类似字符串日期=2013年6月02日等.....

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

我想在升序和降序中对日期字段进行排序

I want to Sort the Date field in Ascending and Descending

可以帮助吗?

我试过这是排序标题字段

I have tried this to Sort Title field

按标题排序可使用以下代码:

Sort by title works with follow code:

    public void sortByTitle() {
        Comparator<Show> comperator = new Comparator<Show>() {
            @Override
            public int compare(Show object1, Show object2) {
                return object1.getTitle().compareToIgnoreCase(
                        object2.getTitle());
            }
        };
        Collections.sort(dataFromDB, comperator);
        lv1.setAdapter(new CustomListViewAdapter(this, dataFromDB));
    }


推荐答案

试试这个...

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.

更新:
更新后的DateFormat检查它..

UPDATED : Updated DateFormat check it..

这篇关于Java排序日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆