-1

私は2つの日付を検証する必要があります。1つはユーザーが選択し、もう1つはカレンダーから取得したものです...これは私がこれまで成功せずに試したことです:

Calendar c = Calendar.getInstance();
if(!DateIsOlder(txtVwAppointmentDate.getText(), c.getTime())) {
            try {
                  //My code
                }  

public static boolean DateIsOlder(CharSequence date1, Date date2){
    //SimpleDateFormat Date2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz -HH:mm yyyy", Locale.ENGLISH);
    SimpleDateFormat dfDate = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH);
    boolean isOlder = false;
    try{
        //Date newDate = (Date)Date2.parse(date2);
        if(dfDate.parse(String.valueOf(date1)).before(dfDate.format(date2))){
        isOlder = true;
        }
        else if (dfDate.parse(String.valueOf(date1)).equals(dfDate.format(date2))){
        isOlder = false;
        }
        else {
            isOlder = false;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return isOlder;
}

エラーは .before(dfDate.format(date2)) にあり、次のように表示されます: Error:(484, 51) error: method before in class Date cannot be applied to given types; 必須: 見つかった日付: 文字列 理由: 実引数文字列はメソッド呼び出しの変換で日付に変換できません

ご覧のとおり、両方の日付が等しい場合に両方の日付を比較すると、コンパイラはこの行にエラーを表示しません。

else if (dfDate.parse(String.valueOf(date1)).equals(dfDate.format(date2)))

また、日付に変更しようとしましたが、成功しません...この問題を解決するのに役立つアイデアはありますか? ありがとう。

4

4 に答える 4

0

解決策を見つけるのを手伝ってくれた皆さんに感謝します。私は簡単な方法を試してみましたが、うまくいきました。calendar.getTime がもたらすすべての情報は必要ないので、次のようにします。

Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        if(!DateIsOlder(txtVwAppointmentDate.getText(), month + "/" + day + "/" + year)) {
            try { //my code }

public static boolean DateIsOlder(CharSequence date1, String date2){
    SimpleDateFormat dfDate = new SimpleDateFormat("MM/dd/yyyy");
    boolean isOlder = false;
    try{
        //Date newDate = (Date)Date2.parse(date2);
        if(dfDate.parse(String.valueOf(date1)).before(dfDate.parse(date2))){
            isOlder = true;
        }
        else {
            isOlder = false;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return isOlder;
}

問題なく動作します。皆さんに感謝します:)

于 2015-02-17T22:43:20.973 に答える