简体   繁体   中英

How do I create a default constructor including a Date as type Date?

In my Java College Class, I have to create the class Customer. A customer has to have a first name, the last name, a birthday and an address.

The problem I have is to fill the default constructor since I have to assign a value to birthday . But I don't know how to do it. If I try to write birthday = (1999,1,1) it throws an error and asks me if I want to convert birthday to int .

My code:

import java.util.Date;

public class Customer {
    private String firstName, lastName;
    private Date birthday;
    private String address;

    public Customer() {
        firstName = "Hans";
        lastName = "Meier";
        //birthday = ? 
        address = "-";
    }

    public Customer(String firstName, String lastName, Date birthday, String address) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthday = birthday;
        this.address = address;
    }

    public Customer(Customer customer) {
        firstName = customer.firstName;
        lastName = customer.lastName;
        birthday = customer.birthday;
        address = customer.address;
    }
}

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API .

If you do not have to deal with timezone, you can use LocalDate :

private LocalDate birthday;

and then, you can use

birthday = LocalDate.of(1999, 1, 1);

If you want to put timezone information, you can use ZonedDateTime eg

ZoneId zoneId = ZoneId.of("Europe/London");
ZonedDateTime birthday = ZonedDateTime.of(LocalDateTime.of(1999, 1, 1, 22, 10), zoneId);

Learn more about the modern date-time API from Trail: Date Time .

FYI: Most of the methods, including the constructors, of java.util.Date are deprecated . If you want to create an object of Date with some given year, month and day, you should use Calendar as shown below:

Calendar calendar = Calendar.getInstance();
calendar.set(1999, 0, 1);
Date birthday = calendar.getTime();

Note that the month in java.util date-time API is 0 -based ie for January , you have to use 0 , for February , you have to use 1 and so on.

By default constructor, I think you mean having a default value for one of the parameters. This can be done by overloading the constructor. You can have multiple constructors for the same class. Something like this:

public class Customer {
    private String firstName, lastName;
    private Date birthday;
    private String address;

    public Customer() {
        firstName="Hans";
        lastName = "Meier";
        birthday = new Date(915195600000l);
        address = "-";
    }

    public Customer(String firstName, String lastName, String address) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthday = new Date(915195600000l);
        this.address = address;
    }

    public Customer (String firstName, String lastName, Date birthday, String address) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthday = birthday;
        this.address = address;
    }

    public Customer (Customer customer) {
        firstName = customer.firstName;
        lastName = customer.lastName;
        birthday = customer.birthday;
        address = customer.address;
    }
}

Keep in mind that the Date takes the epoch time as the parameter. You can also use the other constructors (yyyy, MM, dd) one for example. If you want to convert from string, let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM