gpt4 book ai didi

java - 如何显示我的日期?

转载 作者:行者123 更新时间:2023-12-01 21:38:48 24 4
gpt4 key购买 nike

我的代码无法成功运行,而是出现以下消息:

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;
}
}

最佳答案

问题出在这一行:

String outputDate = outputFormat.format(date);

您正在尝试格式化字符串,而它应该是日期对象。由于您在 try block 内声明了 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

关于java - 如何显示我的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36665937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com