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(たとえば を使用して) に変換する必要がありますSimpleDateFormat。または、理想的にはJoda Timeを使用し、両方が同じ型である場合は値を比較します。

編集:コメントで述べたように、これはあなたが望むものでもありません:

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

mmは月単位ではなく分単位を意味します。代わりに欲しい。詳細については、ドキュメントを参照してください。SimpleDateFormatMM/dd/yyyySimpleDateFormat

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

currentDateです。String_ lowDate_ あなたは2つの異なることDateを試みています。compareTo

Date オブジェクトに変換currentDateして呼び出すcompareTo();

于 2013-04-19T19:20:30.190 に答える
1

通常、Type Date のオブジェクトを String と比較します。SimpleDateFormat (または別の形式) を使用して String を Date に変換すると、両方のオブジェクトを比較できるようになります。

Date correctCurrentDate = sdf.parse(currentDate);

テスト プログラムに try/catch を追加するのを避けるために、私は通常次のようなメイン メソッドを書きました。

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

もちろん、本番環境で実行されるプログラムでは、例外を処理できる場所で try/catch を使用する必要があります。しかし、テストプログラムにとっては面倒です。

于 2013-04-19T19:25:50.203 に答える