2012-02-15 12 views
1

割り当てのために、私は請求書日付の30日後の期日を記入しなければなりませんでした。私のプログラムを実行するとき、私は正しい日付を取得していません。私は何が間違っていたのか分かりません。どんな助けもありがとう。期日を作成し、フォーマットクラスからミリ秒の日付が期待した結果を返さない

コード:

// a method that returns the due date 
public Date getDueDate() 
{ 
    Date dueDate = new Date(invoiceDate.getTime() + 
      (30 * 24 * 60 * 60 * 1000)); 
    return dueDate; 
} 

// a method that returns the formatted due date 
public String getFormattedDueDate() 
{ 
    DateFormat shortDueDate = DateFormat.getDateInstance(DateFormat.SHORT); 
    return shortDueDate.format(this.getDueDate()); 
} 

コードgetFormattedDueDateを呼び出すメインクラスから:一般的に

public static void displayInvoices() 
{ 
    System.out.println("You entered the following invoices:\n"); 
    System.out.println("Number\tTotal\tInvoice Date\tDue Date"); 
    System.out.println("------\t-----\t------------\t--------"); 
    double batchTotal = 0; 
    int invoiceNumber = 1; 
    while (invoices.size() > 0) 
    { 
     Invoice invoice = invoices.pull(); 
     System.out.println(invoiceNumber + "\t  " + invoice.getFormattedTotal() 
       + "  " + invoice.getFormattedDate() 
       + "\t  " + invoice.getFormattedDueDate()); 

     invoiceNumber++; 
     batchTotal += invoice.getInvoiceTotal(); 
    } 
+0

あなたはどのような結果を得ましたか?あなたはどんな結果を期待しましたか? – wallyk

+0

請求書の日付は今日で、私は3/15/12を予定していました。返された日付は1/25/12です – gcalan

+0

私は答えを提供しました。他の誰かが同じような問題を抱えている場合に備えて、ここに追加したいと思っていました。請求書日付に30日を追加する計算では、 "(30L * 24 * 60 * 60 * 1000)"と表示されます。私はそれをテストし、これは実際に働いた。私は "L"が何を達成したかは分かりませんが、それを見ています。 – gcalan

答えて

1

それはIMO、そのように日付の計算を実行するために悪いです。これを行う:

 
public Date getDueDate() { 
    Calendar cal = Calendar.getInstance(); 
    cal.setTime(invoiceDate); 
    cal.add(Calendar.DAY_OF_MONTH, 30); 
    return cal.getTime(); 
} 
+0

ありがとう、brettw。私はあなたが提供したコードで遊んでいきます。 – gcalan

関連する問題