我得到了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)
答案 0 :(得分: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();
}
答案 1 :(得分:1)
- 更好地使用Joda
库来比较日期。
- 首先,您需要解析它们。使用DateTimeFormat
:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime dateTime1 = fmt.parseDateTime(string1);
- 然后使用DateTime.isBefore(..)方法对它们进行比较:
if (dateTime1.isBefore(dateTime2))