Android – 如何按日期对arrayList进行排序?

我正在尝试按日期排序我的arrayList 。 我每次收到通知时都会将日期存储在Firebase上,并从那里检索。 我正在使用Collections.sort但我不知道如何实现我的代码,因为我的日期格式以字符串格式的数字开头,如“2017年7月12日上午12:00”。

我在stackoverflow上看到了一些这样的例子,但我不知道如何让它在我的情况下工作。 我将在下面发布截图和我的代码。

日期未排序。

在此处输入图像描述

NotificationFragment.java

 public class NotificationFragment extends Fragment { prepareNotification1(); sortDate(); return v; } private void prepareNotification1() { FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); String userID = user.getUid(); mRef.child("customers").child(userID).child("Notification").addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { Notification menu = dataSnapshot.getValue(Notification.class); notificationList.add(menu); mAdapter.notifyDataSetChanged(); } }); } public void sortDate() { Collections.sort(notificationList, new Comparator() { @Override public int compare(Notification lhs, Notification rhs) { return lhs.getDate().compareTo(rhs.getDate()); } }); mAdapter = new NotificationAdapter(getContext(), notificationList); mRecyclerView.setAdapter(mAdapter); } } 

MyFirebaseMessagingService.java

 public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { Calendar cal = Calendar.getInstance(); String currentDateTimeString = DateFormat.getDateInstance().format(new Date()); SimpleDateFormat df = new SimpleDateFormat("hh:mm a"); String currentTime = df.format(cal.getTime()); String notificationTime = currentDateTimeString + " at " + currentTime; Notification newNotification = new Notification(remoteMessage.getData().get("body"), notificationTime); mRef.child("customers").child(userID).child("Notification").push().setValue(newNotification); } 

Notification.java

 public class Notification { private String message; private String date; public Notification(){ } public Notification(String message, String date){ this.message = message; this.date = date; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } 

考虑将日期存储在Notification作为long (unix时间)或DateLocalDateTime如果您正在使用Java 8支持),并仅在将其显示为UI时将其格式化为String。

据我了解,Notification.getDate()返回String值。 所以,使用

 public static Date toDate(String value) throws ParseException { DateFormat format = new SimpleDateFormat("d MMMM yyyy 'at' HH:mm'am'", Locale.ENGLISH); return format.parse(value); } 

此方法从您的String中删除Date。 只需获取两个日期并使用Date.compareTo方法。

 date1 = toDate(lhs.getDate()); date2 = toDate(rhs.getDate()); date1.compareTo(date2); 

解析日期后进行排序。

 Collections.sort(notificationList, new Comparator() { DateFormat f = new SimpleDateFormat("dd/MM/yyyy '@'hh:mm a"); @Override public int compare(Notification lhs, Notification rhs) { try { return f.parse(lhs.getDate()).compareTo(f.parse(rhs.getDate())); } catch (ParseException e) { throw new IllegalArgumentException(e); } } }) 

如果您的日期是其他格式,请相应地编写DateFormat

而不是在通知模型中设置如下的日期。

 `String notificationTime = currentDateTimeString + " at " + currentTime;` 

你可以节省时间System.currentTimeinMillis(); 然后显示然后将其解析为日期,如下所示

 DateFormat f = new SimpleDateFormat("dd/MM/yyyy hh:mm a"); f.parse(timeStamp);