1

我得到 json 数据,我想在 arrylist 中获取日期和 nulldate 存储。如何在下面的 listview 排序日期中对日期和空日期数据集进行排序。我将所有数据存储在 All_Approval_data_dto 类中。我的代码如下:请检查它,我错过了什么?

static final Comparator<All_Approval_data_dto> byDate = new Comparator<All_Approval_data_dto>() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

        public int compare(All_Approval_data_dto ord1,
                All_Approval_data_dto ord2) {

            java.util.Date d1 = null;
            java.util.Date d2 = null;
            try {
                d1 = sdf.parse(ord1.lastModifiedDate);
                d2 = sdf.parse(ord2.lastModifiedDate);
                if (d1 == null) {
                    return (d2 == null) ? 0 : -1;
                }
                if (d2 == null) {
                    return 1;
                }

            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block`
                e.printStackTrace();
            }

            return (d1.getTime() > d2.getTime() ? -1 : 1); // descending
            // return (d1.getTime() > d2.getTime() ? 1 : -1); //ascending
        }

    };

我在下面的错误:

11-02 11:03:58.861: ERROR/AndroidRuntime(473): FATAL EXCEPTION: main
11-02 11:03:58.861: ERROR/AndroidRuntime(473): java.lang.NullPointerException
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1201)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.text.DateFormat.parse(DateFormat.java:642)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at com.serana.Approval$2.compare(Approval.java:155)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at com.serana.Approval$2.compare(Approval.java:1)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.util.TimSort.sort(TimSort.java:185)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.util.TimSort.sort(TimSort.java:169)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.util.Arrays.sort(Arrays.java:1907)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.util.Collections.sort(Collections.java:1972)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at com.serana.Approval$4.onItemSelected(Approval.java:79)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at android.widget.AdapterView.access$200(AdapterView.java:42)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at android.os.Handler.handleCallback(Handler.java:587)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at android.os.Looper.loop(Looper.java:123)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.lang.reflect.Method.invokeNative(Native Method)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at java.lang.reflect.Method.invoke(Method.java:521)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-02 11:03:58.861: ERROR/AndroidRuntime(473):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

3

请用此代码替换您的 try/catch 块

try {

            if (d1 == null) {
                return (d2 == null) ? 0 : -1;
            }
            if (d2 == null) {
                return 1;
            }

            d1 = sdf.parse(ord1.lastModifiedDate);
            d2 = sdf.parse(ord2.lastModifiedDate);

        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block`
            e.printStackTrace();
        }
于 2012-11-02T06:41:40.607 回答
1

-更好地使用Joda库来比较日期。

-首先你需要解析它们。使用DateTimeFormat

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime dateTime1 = fmt.parseDateTime(string1);

-然后使用 DateTime.isBefore(..) 方法来比较它们:

if (dateTime1.isBefore(dateTime2))

于 2012-11-02T05:46:53.907 回答