1

これは私のコードで、簡単な質問があります

import java.util.*;
import java.io.*;
import type.lib.GlobalCredit;
import type.lib.CreditCard;
import java.text.SimpleDateFormat;


public class eCheck08A

{
public static void main(String[] args)

{
    PrintStream out = System.out;
    Scanner in = new Scanner(System.in);

    GlobalCredit credit1 = new GlobalCredit().getRandom();

    out.print("Enter report range in years ... ");
    int range = in.nextInt();
    out.println("Cards expiring before " + range + " year(s) from now: ");

    SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");

    for (CreditCard cc : credit1)
    {

    Calendar c = Calendar.getInstance();
    c.setTime(cc.getExpiryDate());
    c.add(Calendar.YEAR, range);
    Date newDate = c.getTime();

        if (cc.getExpiryDate().compareTo(newDate) < range)
        {
            if(cc.getExpiryDate().compareTo(newDate) > range)
            {
                out.print("*");
            }
            out.print(cc.getNumber());
            out.println("\t" + sf.format(cc.getExpiryDate()));

        }
    }



}
}

それがどのように見えるべきかの出力:

Enter report range in years ... 3
Cards expiring before 3 years from now:

561561-8 20/11/2015
045645-7 22/02/2017
456462-3 16/04/2013 *
546548-5 19/08/2016

現在の年は 2012 年 人は範囲として '3' を入力します。したがって、2012 年から 2015 年までの年には " * " が必要です。上記の出力のように、2013 には " * " があります。私のIFステートメントで何が間違っているのか教えていただけますか?

4

4 に答える 4

1

compareTo メソッドが期待どおりの結果を返しません。最初の引数が小さい場合は負、大きい場合は正、等しい場合はゼロを返すことが保証されています。

編集:コードが機能するように変更する方法は次のとおりです。

Date now = new Date(System.currentTimeMillis());
Date endDate = new Date(now.getTime());
endDate.SetYear(endDate.getYear() + 3);
if (cc.getExpiryDate().after(now) && cc.ExpiryDate.before(endDate)) {
 // do stuff.
}

エッジケースを処理するように注意する必要があります(間隔の終わりなどを含める必要があります)が、これはアプローチとして行う必要があります。

于 2012-07-25T06:05:37.670 に答える
1

現在の日付 + 範囲cc.getExpiryDate()と比較する場合は、次のようにします。newDate

Calendar c = Calendar.getInstance();
// commenting this line out because getInstance() gives us the current date already
//    c.setTime(cc.getExpiryDate());
c.add(Calendar.YEAR, range);
Date newDate = c.getTime();

これnewDateは、現在の日付から何年も先の「範囲」です。cc.getExpiryDate()これで、値の比較を開始できます。

    // expiry date is BEFORE the date + "range" years ahead
    if (cc.getExpiryDate().compareTo(newDate) < 0)
    {
        // the expiry date is AFTER or ON the current date
        if(cc.getExpiryDate().compareTo(new Date()) >= 0)
        {
            out.print("*");
        }
    }
    out.print(cc.getNumber());
    out.println("\t" + sf.format(cc.getExpiryDate()));
于 2012-07-25T06:12:44.587 に答える
1

あなたの論理全体がオフになっていると思います。today + nYearsではなく である日付に基づいて、クレジット カードの有効期限を比較する必要がありますexpiryDate + nYears

を見てくださいDate.after、、Date.equalsDate.before

于 2012-07-25T06:13:03.290 に答える
0

Date.compareTo() メソッドのJavaドキュメントを見てください...

戻り値: 引数 Date がこの Date と等しい場合は値 0。この Date が Date 引数より前の場合は 0 未満の値。この Date が Date 引数より後の場合は 0 より大きい値。

しかし、これは年の違いを提供しません。-1、0、または 1 のみが返されます。

解決策として、日付から年を抽出して比較する必要があります。

于 2012-07-25T06:15:41.327 に答える