如何显示我的日期?

时间:2016-04-16 15:19:35

标签: java

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

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

代码

order by

2 个答案:

答案 0 :(得分:2)

问题出在这一行:

String outputDate = outputFormat.format(date);

您正在尝试格式化String,而它应该是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

答案 1 :(得分:1)

您的错误在这里,并不容易找到......:

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

让我们逐步分析:

Date date对象在try catch范围内,而与全局声明的 String date对象(全局声明的String)完全不同,所以你的语句:

 String outputDate = outputFormat.format(date);

正在格式化String,因为您认为要格式化的日期对象不是Try-Catch中的日期对象但是日期:字符串全局定义...

有些人称之为影子效果,但从技术上讲,这只是一个范围问题...