私のコードは正常に実行することができず、代わりに次のメッセージが表示されますどのようにして日付を表示できますか?
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;
}
}
ご協力いただきありがとうございますが、outputDateを表示するにはどうすればよいですか? System.out.println( "Date:" +?); – micky
tryブロックの中から 'System.out.println(" Date: "+ outputDate)'を使うか、ブロックの外側に 'outputDate'を定義して、その中に値を入れて使いたいところで使用してください。編集した回答を確認してください。 – dambros
ありがとう、私は最後にtry catchを理解していると思う – micky