简体   繁体   English

如何创建并使用字符串作为输入输出Date对象? 即“ 13/04/1998”

[英]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.

相关问题 如何创建一个在运行时接受选项的Java项目(即-p或-f) - How do I create a java project that takes options at runtime (i.e. -p or -f) 我如何阅读(即理解)此Java数组? - How do I read (i.e. understand) this Java array? 使用String的JAVA 2D地图? 即“ +”和“ C” - JAVA 2D Map using String? i.e. “+” and “C” 如何直接从另一个类访问变量(即不使用类名或对象)? - How to access variable from another class directly(i.e. without using class name or object)? 如何获得对象e1的正确输出? - How do I get the correct output for the object e1? 识别对象类型(即类) - Identify Object Type(i.e. Class) Java创建dayOfWeek的日期和没有特定日期的时间,即对任何星期一有效 - java create date for dayOfWeek and time without a specific date, i.e. valid for any Monday 如何使用正则表达式在字符串(即[12] [12])中找到连续的重复,但长度不小于2? - How do I use a regex to find a consecutive repeat in a string (i.e. [12][12]) but only of length 2 or greater? 将字符串转换为对象名称。 IERstring.objectname - Convert string to object name. I.E. R.string.objectname 如何使用Spring转换服务将字符串值从属性文件自动转换为其他数据类型,即持续时间? - How to auto convert string values from properties files to other data types i.e. duration using spring conversion service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM