在Java中如何按日期对ArrayList的元素排序?

在Java中如何按日期对ArrayList的元素排序?,java,arraylist,Java,Arraylist,我创建了一个待办事项列表程序,记录用户输入的任务。对于每个任务,用户必须输入名称、日期等 当用户从菜单中选择“5”时,程序将按日期对这些任务进行排序。我需要根据任务日期和时间的升序对所有任务进行排序,即日期和时间较早的任务将列在日期和时间较晚的任务之前,并显示排序后的列表 但是,当我运行代码时,我会收到以下错误: Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given

我创建了一个待办事项列表程序,记录用户输入的任务。对于每个任务,用户必须输入名称、日期等

当用户从菜单中选择“5”时,程序将按日期对这些任务进行排序。我需要根据任务日期和时间的升序对所有任务进行排序,即日期和时间较早的任务将列在日期和时间较晚的任务之前,并显示排序后的列表

但是,当我运行代码时,我会收到以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.base/java.text.DateFormat.format(DateFormat.java:338)
at java.base/java.text.Format.format(Format.java:158)
at ToDoList.lambda$0(ToDoList.java:238)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at ToDoList.sortTasks(ToDoList.java:238)
at ToDoList.main(ToDoList.java:106)
这是我到目前为止的代码:(底部是
sortTasks()

import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.Scanner;
课堂任务{
私有字符串标题;
非公开日期;
私有字符串时间;
私有字符串位置;
私有字符串的持续时间;
私有字符串分类;
SimpleDataFormat格式=新的SimpleDataFormat(“dd/MM/yyyy”);
任务(字符串标题、日期、字符串时间、字符串位置、字符串持续时间、字符串类别){
标题=标题;
日期=日期;
时间=时间;
位置=位置;
持续时间=持续时间;
类别=类别;
}
公共字符串getTitle(){
返回标题;
}
公共日期getDate(){
返回日期;
}
公共字符串getTime(){
返回时间;
}
公共字符串getLocation(){
返回位置;
}
公共字符串getDuration(){
返回时间;
}
公共字符串getCategory(){
返回类别;
}
公共字符串getItem(){
返回标题+”、“+格式.格式(日期)+”、“时间+”、“+位置+”、“+持续时间+”、“+类别”;
}
}
公营托达利斯特{
公共任务myTaskObj;
SimpleDataFormat格式=新的SimpleDataFormat(“dd/MM/yyyy”);
private static List currentList=new ArrayList();
公共托多利斯特(){
}
公共静态void main(字符串[]args)引发异常{
ToDoList listObj=新ToDoList();
int menuItem=-1;
while(menuItem!=7){
menuItem=listObj.printMenu();
开关(菜单项){
案例1:
listObj.addItem();
打破
案例2:
listObj.removietem();
打破
案例3:
listObj.removeAllTasks();
打破
案例4:
listObj.showList();
打破
案例5:
listObj.sortTasks();
打破
案例6:
listObj.searchTasks();
打破
案例7:
System.out.println(“再见!”);
违约:
System.out.println(“输入有效选项”);
}
}   
}
公共int打印菜单(){
扫描仪=新的扫描仪(System.in);
System.out.println();
System.out.println(“-------------------------”);
System.out.println(“主菜单”);
System.out.println(“-------------------------”);
System.out.println(“1.添加任务”);
System.out.println(“2.删除任务”);
System.out.println(“3.删除所有任务”);
System.out.println(“4.列出所有任务”);
System.out.println(“5.按日期排序任务”);
System.out.println(“6.搜索任务”);
System.out.println(“7.退出程序”);
System.out.println();
System.out.print(“输入选项:”);
int choice=scanner.nextInt();
回报选择;
}
公开作废展示清单(){
System.out.println();
System.out.println(“-------------------------”);
System.out.println(“待办事项列表”);
System.out.println(“-------------------------”);
整数=0;
用于(字符串项:currentList){
System.out.println(++编号+“+”项);
}
System.out.println(“-------------------------”);
}
public void addItem()引发异常{
System.out.println(“添加任务”);
System.out.println(“-------------------------”);
System.out.print(“输入任务标题:”);
扫描仪=新的扫描仪(System.in);
字符串标题=scanner.nextLine();
系统输出打印(“输入任务日期(dd/mm/yyyy):”;
扫描仪scanner2=新扫描仪(System.in);
Date=format.parse(scanner2.next());
System.out.print(“输入任务时间:”);
扫描仪scanner3=新扫描仪(System.in);
字符串时间=scanner3.nextLine();
System.out.print(“输入任务位置:”);
扫描仪scanner4=新扫描仪(System.in);
字符串位置=scanner4.nextLine();
System.out.println(“输入任务持续时间(可选-按Enter键跳过):”;
扫描仪scanner5=新扫描仪(System.in);
字符串持续时间=scanner5.nextLine();
System.out.println(“输入任务类别(可选-按Enter键跳过):”;
扫描仪scanner6=新扫描仪(System.in);
String category=scanner6.nextLine();
myTaskObj=新任务(标题、日期、时间、位置、持续时间、类别);
字符串item=myTaskObj.getItem();
currentList.add(item);
System.out.println(“添加任务!”);
}
公共无效删除项(){
System.out.println(“删除任务”);
System.out.println(“-------------------------”);
扫描仪=新的扫描仪(System.in);
System.out.print(“您想要删除什么?(输入编号):”;
int index=scanner.nextInt();
如果((索引-1)currentList.size()){
System.out.println(“错误的索引号!请输入1到“+currentList.size()”);
}否则{
当前列表。删除(索引-1);
}
System.out.println(“-------------------------”);
System.out.println(“任务已删除!”);
}
public void removeAllTasks(){
System.out.println(“删除所有任务”);
System.out.println(“-------------------------”);
showList();
扫描仪键盘=新扫描仪(System.in);
System.out.print(“是否确实要删除所有任务?“是”或“否”:”);
字符串选择=keyboard.nextLine();
如果(选择等于(“是”)){
currentList.removeAll(currentList);
System.out.println(“删除所有任务!”);
}
其他的
如果(选择等于(“否”))
System.out.println(“未删除的任务”);
}
公共空间
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner; 

class Task{

private String theTitle;
private Date theDate;
private String theTime;
private String theLocation;
private String theDuration;
private String theCategory;
SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy");

Task(String title, Date date, String time, String location, String duration, String category) {
    
    theTitle = title;
    theDate = date;
    theTime = time;
    theLocation = location;
    theDuration = duration;
    theCategory = category;
    
}

public String getTitle() {
    
    return theTitle;
}

public Date getDate() {

    return theDate;
}

public String getTime() {
    
    return theTime;
}

public String getLocation() {
    
    return theLocation;
}

public String getDuration() {
    
    return theDuration;
}

public String getCategory() {
    
    return theCategory;
}

public String getItem() {
    
    return theTitle + ", " + format.format(theDate) + ", " + theTime + ", " + theLocation + ", " + theDuration + ", " + theCategory;
}

}


public class ToDoList {

public Task myTaskObj;
SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy");
private static List<String> currentList = new ArrayList<String>();

public ToDoList() {
    
}

public static void main (String[] args) throws ParseException {
    
    ToDoList listObj = new ToDoList();
    
    int menuItem = -1;
    while (menuItem != 7) {
        menuItem = listObj.printMenu();
        switch (menuItem) {
        case 1:
            listObj.addItem();
            break;
        case 2:
            listObj.removeItem();
            break;
        case 3:
            listObj.removeAllTasks();
            break;
        case 4:
            listObj.showList();
            break;
        case 5: 
            listObj.sortTasks();
            break;
        case 6:
            listObj.searchTasks();
            break;
        case 7: 
            System.out.println("Goodbye!");
        default:
            System.out.println("Enter a valid option");
        }
    }   
    
}

public int printMenu() {
    
    Scanner scanner = new Scanner(System.in);
    System.out.println();
    System.out.println("----------------------");
    System.out.println("Main Menu");
    System.out.println("----------------------");
    System.out.println("1. Add a task");
    System.out.println("2. Delete a task");
    System.out.println("3. Delete all tasks");
    System.out.println("4. List all tasks");
    System.out.println("5. Sort tasks by date");
    System.out.println("6. Search for a task");
    System.out.println("7. Exit the program");
    System.out.println();
    System.out.print("Enter choice: ");
    int choice = scanner.nextInt();
    
    return choice;
    
}

public void showList() {
System.out.println();
System.out.println("----------------------");       
System.out.println("To-Do List");
System.out.println("----------------------");
int number = 0;
for (String item : currentList) {
    System.out.println(++number + ". " + item);
}
System.out.println("----------------------");


}

public void addItem() throws ParseException {
System.out.println("Add a task");
System.out.println("----------------------");

System.out.print("Enter the task title: ");
Scanner scanner = new Scanner(System.in);
String title = scanner.nextLine();

System.out.print("Enter the task date (dd/mm/yyyy): ");
Scanner scanner2 = new Scanner(System.in);
Date date=format.parse(scanner2.next());


System.out.print("Enter the task time: ");
Scanner scanner3 = new Scanner(System.in);
String time = scanner3.nextLine();

System.out.print("Enter the task location: ");
Scanner scanner4 = new Scanner(System.in);
String location = scanner4.nextLine();

System.out.println("Enter the task duration (optional - press enter to skip): ");
Scanner scanner5 = new Scanner(System.in);
String duration = scanner5.nextLine();

System.out.println("Enter the task category (optional - press enter to skip): ");
Scanner scanner6 = new Scanner(System.in);
String category = scanner6.nextLine();

myTaskObj = new Task(title, date, time, location, duration, category);

String theItem = myTaskObj.getItem();

currentList.add(theItem);
System.out.println("Task Added!");



}

public void removeItem() {
System.out.println("Delete a task");
System.out.println("----------------------");
Scanner scanner = new Scanner(System.in);
System.out.print("What do you want to remove? (Enter number): ");
int index = scanner.nextInt();
if((index-1)<0 || index>currentList.size()) {
    System.out.println("Wrong index number! Please enter in range of 1 to "+currentList.size());            
}else {
    currentList.remove(index-1);
}
System.out.println("----------------------");
System.out.println("Task Removed!");


}

public void removeAllTasks() {

System.out.println("Remove all tasks");
System.out.println("----------------------");
showList();

Scanner keyboard = new Scanner(System.in);
System.out.print("Are you sure you'd like to delete all tasks? 'Yes' or 'No': ");
String choice = keyboard.nextLine();
if(choice.equals("Yes")) {
    currentList.removeAll(currentList);
    System.out.println("All tasks deleted!");
}
else 
    if(choice.equals("No"))
    System.out.println("Tasks not deleted");

}

public void sortTasks() {

System.out.println("Sorted tasks by date (earliest first): ");

Collections.sort(currentList);

currentList.forEach(action-> System.out.println(format.format(action)));
}

}
class ToDoList {
    private final List<Task> tasks = new ArrayList<>();
    ...
}
public void sortTasks(Comparator<Task> order) {
    tasks.sort(order);
}
case 5: toDoList.sortTasks(Comparator.comparing(Task::getDate).thenComparing(Task::getTime));
public ZonedDateTime projectedStartingMoment ( )
{
    ZonedDateTime zdt = this.startDateTime.atZone( this.zoneId );
    return Objects.requireNonNull( zdt );
}
// Implement `Comparable` interface.
@Override
public int compare ( Task task1 , Task task2 )
{
    return task1.projectedStartingMoment().compareTo( task2.projectedStartingMoment() );
}
List < Task > tasksSorted = this.toDoList.getTasksSortedByProjectedStartingMoment();