[英]How do I create and then output a Date object using a String as the input? i.e. “13/04/1998”
This setup is not working. 此设置无效。 When I print out v1 I get an incorrect date in the wrong format as it includes the time as 00:00:0000. 当我打印出v1时,我得到的日期格式错误,因为它包含的时间为00:00:0000。
Is there a way of overriding the Date objects methods for formatting (if there is any)? 有没有一种方法可以覆盖Date对象的格式化方法(如果有的话)?
public class Vehicle
{
//**********Declarations**********
private String manufacturer;
private String model;
//********** Declaring Customer object **********
private Customer customer = null;
private String VIN;
private Date dateOfManufacture;
private Date dateOfSale = null;
private Boolean sold = false;
private char taxBand;
private int costOfVehicle;
//**********Constructor**********
public Vehicle(String man, String mod, Customer custName, String VIN, String dateOfMan, char taxBand, int costOfVehicle)
{
this.manufacturer = man;
this.model = mod;
this.customer = custName;
this.VIN = VIN;
Date newDate = new Date(dateOfMan);
this.dateOfManufacture = newDate;
this.taxBand = taxBand;
this.costOfVehicle = costOfVehicle;
}
public Date getDateOfManufacture()
{
return dateOfManufacture;
}
public void setDateOfManufacture(Date dateOfManufacture)
{
this.dateOfManufacture = dateOfManufacture;
}
public Date getDateOfSale()
{
return dateOfSale;
}
public void setDateOfSale(Date dateOfSale)
{
this.dateOfSale = dateOfSale;
}
}
//*******Driver class creating a new Vehicle object *******
Vehicle v1 = new Vehicle("Ford", "Fiesta", null, "123abc", "12/03/2000", 'c', 3000);
to get the Date
from String
you can use SimpleDateFormat
like this: 要从String
获取Date
,可以使用SimpleDateFormat
如下所示:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyy");
Date myDate = simpleDateFormat.parse("13/04/1998");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.