gpt4 book ai didi

java - CompareTo 不能应用于 java.util.Date (Java)

转载 作者:行者123 更新时间:2023-12-01 06:45:30 25 4
gpt4 key购买 nike

我正在用 Java 编写约会程序,遇到如下错误:

AppointmentNew.java:68: unreported exception java.text.ParseException; must be caught or declared to be thrown
Date lowDate = sdf.parse(stdin.nextLine());
^
AppointmentNew.java:70: unreported exception java.text.ParseException; must be caught or declared to be thrown
Date highDate = sdf.parse(stdin.nextLine());
^
AppointmentNew.java:77: unreported exception java.text.ParseException; must be caught or declared to be thrown
Date newCurrentDate = sdf.parse(currentDate);

这个程序应该允许用户进行新的约会,当他们这样做时,他们就可以输入日期和描述(他们可以根据需要多次执行此操作),之后他们可以选择一个程序打印的日期范围(它应该打印出他们提供的日期范围内的约会”。这就是我的错误发生的地方......

这是我的代码:

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AppointmentNew
{
public static void main (String[] args)
{
ArrayList<String> list = new ArrayList<String>();
Scanner stdin = new Scanner(System.in);
String choice = "";
int choiceNum = 0;
String date = "";
String descrip = "";
int type = 0;
String typeChose = "";

System.out.println("Welcome to Appointment App!\n");
System.out.println("\t============================");

do
{
System.out.print("\n\tMake Choice (1: New, 2: Print Range, 3: Print All, 4: Quit) ");
choice = stdin.nextLine();
choiceNum = Integer.parseInt(choice);

if (choiceNum == 1)
{
System.out.print("\n\n\tEnter New Appointment Date in mm/dd/yyyy format: ");
date = stdin.nextLine();

System.out.print("\n\n\tEnter New Appointment Description: ");
descrip = stdin.nextLine();

System.out.print("\n\n\tEnter Type (1 = Once, 2 = Daily, 3 = Monthly): ");
type = stdin.nextInt();
stdin.nextLine();

if (type == 1)
{
Once once = new Once(date, descrip);
typeChose = "One-Time";
}
else if (type == 2)
{
Daily daily = new Daily(date, descrip);
typeChose = "Daily";
}
else
{
Monthly monthly = new Monthly(date, descrip);
typeChose = "Monthly";
}
String stringToAdd = "";
stringToAdd = (date + " : \"" + descrip + "\", " + typeChose);
list.add(stringToAdd);

System.out.println("\n\n\tNew " + typeChose + " Appointment Added for " + date + "\n");
System.out.println("\t============================\n");


}

if (choiceNum == 2)
{
System.out.print("\n\n\tEnter START Date in mm/dd/yyyy format: ");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date lowDate = sdf.parse(stdin.nextLine());
System.out.print("\n\n\tEnter END Date in mm/dd/yyyy format: ");
Date highDate = sdf.parse(stdin.nextLine());

for(int i = 0; i < list.size(); i++)
{
int dateSpot = list.get(i).indexOf(" ");
String currentDate = list.get(i);
currentDate.substring(0, dateSpot);
Date newCurrentDate = sdf.parse(currentDate);

if (newCurrentDate.compareTo(lowDate) >= 0 && newCurrentDate.compareTo(highDate) <= 0)
{
System.out.println("\n\t" + list.get(i));
}
}
}

if (choiceNum == 3)
{
for(int i = 0; i < list.size(); i++)
{
System.out.println("\n\t" + list.get(i));
}
}

}while (choiceNum != 4);
}
}

最佳答案

问题是您正在尝试将字符串与日期进行比较。这到底意味着什么?

您应该将String转换为Date(例如使用SimpleDateFormat) - 或者理想情况下,使用Joda Time - 当您获得相同类型时,然后比较这些值。

编辑:正如评论中所述,这也不是您想要的:

SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");

mm 表示 SimpleDateFormat 中的分钟,而不是。您需要使用MM/dd/yyyy。请参阅SimpleDateFormat documentation了解更多详情。

关于java - CompareTo 不能应用于 java.util.Date (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16112009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com