0

我正在用 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);      
 }
}
4

3 回答 3

7

问题是您正在尝试将字符串与日期进行比较。那有什么意思?

您应该将 转换StringDate(例如 with SimpleDateFormat) - 或者理想情况下,使用Joda Time - 然后在将它们都作为相同类型时比较值。

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

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

mm表示分钟SimpleDateFormat而不是几个月。你想要MM/dd/yyyy。有关更多详细信息,请参阅SimpleDateFormat文档

于 2013-04-19T19:20:23.290 回答
2
currentDate.compareTo(lowDate)

currentDateStringlowDateDate。你正在尝试compareTo两种不同的东西。

转换currentDate为 Date 对象,然后调用compareTo();

于 2013-04-19T19:20:30.190 回答
1

这很正常,您将日期类型的对象与字符串进行比较。使用 SimpleDateFormat(或其他格式)将您的字符串转换为日期,您将能够比较这两个对象。

Date correctCurrentDate = sdf.parse(currentDate);

为了避免在测试程序中添加 try/catch,我一般写了 main 方法,如:

public static void main (String[] args) throws Exception

当然,对于在生产中运行的程序,您应该使用 try/catch 来处理异常。但这对于测试程序来说很烦人。

于 2013-04-19T19:25:50.203 回答