Java 首先按照最近或即将到来的日期的顺序获得一个排序列表,然后根据今天的情况获得剩余的日期';日期
列表的排序方式应确保项目的日期应接近今天的日期,如果已经过了今天的日期,并且仍然是最近的日期,则应排到末尾。 例如: 日期-2017年10月3日2017年9月12日2017年10月20日2017年10月21日 假设这是从数据库检索到的日期列表,当添加到自定义适配器时,将以相同的顺序显示listview项。 但我希望订购日期为(考虑到今天的日期为-18/10/2017) 2017年10月20日 21/10/2017 (由于即将到来的日期在前两项中结束,现在是过去的日期。) 日期列表)2017年10月3日2017年9月12日 下面是使用的类 AssignmentUpload.javaJava 首先按照最近或即将到来的日期的顺序获得一个排序列表,然后根据今天的情况获得剩余的日期';日期,java,android,list,sorting,date,Java,Android,List,Sorting,Date,列表的排序方式应确保项目的日期应接近今天的日期,如果已经过了今天的日期,并且仍然是最近的日期,则应排到末尾。 例如: 日期-2017年10月3日2017年9月12日2017年10月20日2017年10月21日 假设这是从数据库检索到的日期列表,当添加到自定义适配器时,将以相同的顺序显示listview项。 但我希望订购日期为(考虑到今天的日期为-18/10/2017) 2017年10月20日 21/10/2017 (由于即将到来的日期在前两项中结束,现在是过去的日期。) 日期列表)2017年10
public AssignmentUpload(String fileName, String url, String year, String title,
String instructions, AttendanceDate attendanceDate, String subjectName, String uploadedBy) {
this.fileName = fileName;
this.url = url;
this.year = year;
this.title = title;
this.instructions = instructions;
this.attendanceDate = attendanceDate;
this.subjectName = subjectName;
this.uploadedBy = uploadedBy;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public AttendanceDate getAttendanceDate() {
return attendanceDate;
}
public void setAttendanceDate(AttendanceDate attendanceDate) {
this.attendanceDate = attendanceDate;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public String getUploadedBy() {
return uploadedBy;
}
public void setUploadedBy(String uploadedBy) {
this.uploadedBy = uploadedBy;
}}
AttendanceDate.java用于日期
public class AttendanceDate implements Serializable{
private int day, month, year;
public AttendanceDate() {
}
public AttendanceDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "AttendanceDate{" +
"day=" + day +
", month=" + month +
", year=" + year +
'}';
}}
FacultyViewSignmentActivity.java具有要编辑的loadAssignmentList()函数
private void loadAssignmentsList() {
pDialog.setTitle("Searching...");
pDialog.setMessage("Enjoy Assignments... :)");
pDialog.show();
reference.child("assignments")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
assignmentUploadList.clear();
for (DataSnapshot ps : dataSnapshot.getChildren()) {
for (DataSnapshot ds : ps.getChildren()) {
assignmentYear.setSubjectName(ds.getValue().toString());
for (DataSnapshot dss : ds.getChildren()) {
AssignmentUpload a = dss.getValue(AssignmentUpload.class);
assignmentUploadList.add(a);
}
}
}
assAdapter = new FacultyViewAssignmentAdapter(FacultyViewAssignmentActivity.this, assignmentUploadList);
recyclerView.setAdapter(assAdapter);
pDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
pDialog.dismiss();
}
});
}
我已经坚持了很长时间,任何帮助都将不胜感激。。。
谢谢 如果创建两个排序列表,一个是即将到来的日期,另一个是已经过去的日期,该怎么办。然后,首先用即将到来的日期,然后用过去的日期填充第三个列表: TestCode.java
package com.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class TestCode {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date today = sdf.parse("18/10/2017");
// assuming Dates - 3/10/2017 12/9/2017 20/10/2017 21/10/2017
String dts = "3/10/2017,12/9/2017,20/10/2017,21/10/2017";
List<AssignmentUpload> assignmentUploadList = new ArrayList<AssignmentUpload>();
List<AssignmentUpload> futureAssg = new ArrayList<AssignmentUpload>();
List<AssignmentUpload> pastAssg = new ArrayList<AssignmentUpload>();
List<AssignmentUpload> finalAssg = new ArrayList<AssignmentUpload>();
for (String dt : dts.split(",")) {
String[] nos = dt.split("/");
int day = Integer.parseInt(nos[0]);
int mon = Integer.parseInt(nos[1]);
int year = Integer.parseInt(nos[2]);
AttendanceDate ad = new AttendanceDate(day,mon,year);
AssignmentUpload au = new AssignmentUpload();
au.setAttendanceDate(ad);
au.setSubjectName("assg - " +dt);
assignmentUploadList.add(au);
}
// Now we will start with assignmentUploadList
for (AssignmentUpload au : assignmentUploadList) {
int day = au.getAttendanceDate().getDay();
int mon = au.getAttendanceDate().getMonth();
int year = au.getAttendanceDate().getYear();
Date date = sdf.parse(day+"/"+mon +"/"+year);
if (date.before(today)) {
pastAssg.add(au);
} else {
futureAssg.add(au);
}
}
// Sort pastand future dates individually
Collections.sort(futureAssg);
Collections.sort(pastAssg,Collections.reverseOrder());
// Add future Dates to final Dates first
finalAssg.addAll(futureAssg);
// Then Add past Dates to final Dates
finalAssg.addAll(pastAssg);
assignmentUploadList = finalAssg;
for (AssignmentUpload au : assignmentUploadList) {
System.out.println(au.getSubjectName());
}
}
}
输出:
助理-20/10/2017
助理-21/10/2017
助理-3/10/2017
assg-2017年12月9日您需要按照自己的意愿对日期进行排序的比较器如下所示:
Comparator<LocalDate> c = (date1, date2) -> {
boolean isPast1 = date1.isBefore(LocalDate.now());
boolean isPast2 = date2.isBefore(LocalDate.now());
if (isPast1 != isPast2)
return isPast1 ? 1 : -1;
return isPast1 ? date2.compareTo(date1) : date1.compareTo(date2);
});
使用上面的比较器(即第一次覆盖过去和将来,然后对每个组排序),你要求上传的列表被他们的日期排序。你可以考虑用比较或比较的方式来排序你的列表,因为日期的数据不在日期格式,它被简单地划分为整数,如整数日、整数月、整数年。。。。如果你能告诉我一个更好的方法使用比较和比较,然后请。。。但是当我尝试这个方法时,月和日的顺序是上升的,但是它们的组合是混乱的。你不能用这些数字创建
LocalDate
s吗?它应该有工厂的方法。然后你可以比较它们。类似于uploads.sort(Comparator.comparating(upload->createLocalDateFromAttDate(upload.getAttendanceDate()))的东西,但它只会以升序或降序的方式返回列表,而不会像我上面解释的那样以最接近的日期为准@Maltehartwig看到我的答案了。如果你使用新的ComparatorAPI,一旦你知道如何使用它,这真的很容易。谢谢,现在我有我的日期排序,以未来,过去,然后我添加到最终。但是你能告诉我如何将结果与日期的相应记录同时更新到我的任务上传列表中吗!看来我能回答你最初的问题。你能把我的答案标记为正确吗?我正在查看您的代码,很快就会有答案。您是否希望您的AssignmentUploadList中的AssignmentUpload对象按照与日期相似的顺序进行排序?是的,问题是关于按日期对列表进行排序,无法获得日期本身的排序列表。@MalteHartwig我提供的方法也可以用于对assignmentUploadList进行排序。(1) 我们将此assignmentUploadList分为两个列表,一个是过去的任务列表,另一个是未来的任务列表。(2) 我们将构建两个比较器,按照升序日期和降序日期对分配上载对象进行排序。(3) 我们将按降序排列过去的作业,按升序排列未来的作业(4),然后将未来和过去的作业添加到新列表中。(5) 最后我们会说assignmentUploadList=
Comparator<LocalDate> c = (date1, date2) -> {
boolean isPast1 = date1.isBefore(LocalDate.now());
boolean isPast2 = date2.isBefore(LocalDate.now());
if (isPast1 != isPast2)
return isPast1 ? 1 : -1;
return isPast1 ? date2.compareTo(date1) : date1.compareTo(date2);
});
uploads.sort(Comparator.comparing(upload -> LocalDate.of(upload.getAttendanceDate().getYear(), upload.getAttendanceDate().getMonth(), upload.getAttendanceDate().getDay()),
(date1, date2) -> {
boolean isPast1 = date1.isBefore(LocalDate.now());
boolean isPast2 = date2.isBefore(LocalDate.now());
if (isPast1 != isPast2)
return isPast1 ? 1 : -1;
return isPast1 ? date2.compareTo(date1) : date1.compareTo(date2);
}));