2016-04-16 7 views
0

私のコードは正常に実行することができず、代わりに次のメッセージが表示されますどのようにして日付を表示できますか?

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date 
    at java.text.DateFormat.format(DateFormat.java:310) 
    at java.text.Format.format(Format.java:157) 
    at FuelTransaction.displayDetails(FuelTransaction.java:50) 
    at FuelLogger.main(FuelLogger.java:21) 

コード

import java.text.NumberFormat; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import javax.swing.JOptionPane; 

public class FuelLogger 
{ 
    public static void main (String [] arguments) 
     { 

      FuelTransaction Ft1 = new FuelTransaction("05/01/2016", 500, 10, 0.990); 
      FuelTransaction Ft2 = new FuelTransaction("16/01/2016", 560, 10, 0.990); 
      FuelTransaction Ft3 = new FuelTransaction(); 

      Ft3.setDate("29/01/2016"); 
      Ft3.setCarMileage(670); 
      Ft3.setNumberOfLitres(15); 
      Ft3.setCostPerLitre(1.01); 

      Ft1.displayDetails(); 
      Ft2.displayDetails(); 
      Ft3.displayDetails(); 

      //allow up to 100 records 
      if (FuelTransaction.getTotalNum() > 100) 
      { 
       System.exit(0); 
      } 

      System.out.println("The total number of FuelTransactions is " + FuelTransaction.getTotalNum()); 
     } 
} 

public class FuelTransaction 
{ // properties 

    private static int totalNum = 0; 
    private String date; 
    private int carMileage; 
    private int numberOfLitres; 
    private double costPerLitre; 
    private double cost; 

    public FuelTransaction(String inDate, int inCarMileage, int inNumberOfLitres, double inCostPerLitre) 
    { 
     this.date = inDate; 
     this.carMileage = inCarMileage; 
     this.numberOfLitres = inNumberOfLitres; 
     this.costPerLitre = inCostPerLitre; 
     totalNum++; 
    } 


    public FuelTransaction() 
    { 
     totalNum++; 
    } 

    //methods 

    public void displayDetails() 
    { // display results 
     NumberFormat money = NumberFormat.getCurrencyInstance(); 
     money.setMinimumFractionDigits(3); 

     //DateFormat date = DateFormat.getDateInstance(DateFormat.LONG); 
     DateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy"); 
     DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy"); 
     String inputDate = this.date; 
     try 
      { 
      Date date = inputFormat.parse(inputDate); 
      }catch(ParseException e){ 
       e.printStackTrace(); 
      } 
      String outputDate = outputFormat.format(date); 

    // System.out.println("Date: " + date.format(this.date)); 

     System.out.println("CarMileage: " + this.carMileage); 
     System.out.println("Number of litres: " + this.numberOfLitres); 
     System.out.println("Cost per litre: " + money.format(this.costPerLitre)); 
     this.displayCost(); 
    } 

    //Date 

    public String getDate() 
    { 
     return this.date; 
    } 
    public void setDate(String inDate) 
    { 
     this.date = inDate; 
    } 

    //Car mileage 
    public int getCarMileage() 
    { 
     return this.carMileage; 
    } 
    public void setCarMileage(int inCarMileage) 
    { 
     this.carMileage = inCarMileage; 
    } 

    //Number of litres 
    public int getNumberOfLitres() 
    { 
     return this.numberOfLitres; 
    } 
    public void setNumberOfLitres(int inNumberOfLitres) 
    { 
     this.numberOfLitres = inNumberOfLitres; 
    } 

    //Cost per litre 
    public double getCostPerLitres() 
    { 
     return this.costPerLitre; 
    } 
    public void setCostPerLitre(double inCostPerLitre) 
    { 
     this.costPerLitre = inCostPerLitre; 
    } 

    //Number of transactions 
    public static int getTotalNum() 
    { 
     return FuelTransaction.totalNum; 
    } 


    //Calculating 

    public void displayCost() 
    { 
     NumberFormat money = NumberFormat.getCurrencyInstance(); 
     System.out.println("Cost: " + money.format(this.cost())); 
    } 
    private double cost() 
    { 
     double x = this.numberOfLitres; 
     double y = this.costPerLitre; 
     x = x * y; 
      return x; 
    } 
} 

答えて

2

プロblemは次の行にあります。

String outputDate = outputFormat.format(date); 

文字列を、代わりにDateオブジェクトにする必要がある場合は、文字列の書式を設定しようとしています。 tryブロック内でDateオブジェクトを宣言しているので、前の行のスコープの外にあり、Stringを使用します(これは行を避けるために同じ名前であってはなりません)。それを変更します。

String outputDate = null; 
try { 
    Date date = inputFormat.parse(inputDate); 
    outputDate = outputFormat.format(date); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 
System.out.println("Date: " + outputDate); 

事がある、あなたはしかし、何のためにこのoutputDateを使用していません。

+0

ご協力いただきありがとうございますが、outputDateを表示するにはどうすればよいですか? System.out.println( "Date:" +?); – micky

+0

tryブロックの中から 'System.out.println(" Date: "+ outputDate)'を使うか、ブロックの外側に 'outputDate'を定義して、その中に値を入れて使いたいところで使用してください。編集した回答を確認してください。 – dambros

+0

ありがとう、私は最後にtry catchを理解していると思う – micky

1

あなたのエラーが...ここにあり、見つけるのは簡単ではありませんでした:

try 
    { 
     Date date = inputFormat.parse(inputDate); 
    }catch(ParseException e){ 
     e.printStackTrace(); 
    } 
    String outputDate = outputFormat.format(date); 

この手順をステップごとに分析できます。

Date dateオブジェクトがスコープトライキャッチであり、かつを、それがグローバルに宣言String dateオブジェクトと全く異なっているので、あなたの文の(グローバル文字列に宣言):

String outputDate = outputFormat.format(date); 

文字列をフォーマットさを理由日付オブジェクトます...ただスコープの問題があり、

一部の人々は、この影の効果を呼び出す...グローバルに定義された文字列が、技術的に話:あなたは書式設定されていると思うがのtry-catchしかし、日付の内部日付オブジェクトではありません

関連する問題