-2

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;
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Sye
  • 1
  • 1
  • 3
    Classes like this typically don't have default constructors. Why do you think you need one? I'd just leave the class fields empty if I had to provide a default constructor. – markspace Jan 23 '21 at 16:12
  • 3
    `If i try to write birthday = (1999,1,1)` When you invoke a constructor, you must call `new` and the classname. `= new Date( 1999, 1, 1)` – markspace Jan 23 '21 at 16:13
  • Its is spefically asked to do so in the Task. – Sye Jan 23 '21 at 16:15
  • 1
    Including to create some default value? Again I think leaving them blank is better, it shows that the object isn't fully constructed. – markspace Jan 23 '21 at 16:16
  • birthday = new Date (1999,1,1); if i try that i get "The Constructor Date(int,int,int) is depricated" – Sye Jan 23 '21 at 16:16
  • 1
    Yup, you probably shouldn't be using Date like this. Since it's just a class assignment I assumed it didn't matter. It's deprecated but it still works. See an answer below for proper modern usage of `LocaleDate`. But you might want to ask your instructor what kind of API you should be using. – markspace Jan 23 '21 at 16:18
  • 2
    If the birthdate is unknown, it should be set to a `null` value, indicating that there is not date available. Using a dummy date is misleading. – Andreas Jan 23 '21 at 16:21
  • I recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `LocalDate` or another class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 25 '21 at 18:14

2 Answers2

1

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 e.g.

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 i.e. for January, you have to use 0, for February, you have to use 1 and so on.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Why would you want to put timezone information in a birth date? – Andreas Jan 23 '21 at 16:25
  • @Andreas - It depends...an application (horoscope applications) may require to record date of birth with timezone. – Arvind Kumar Avinash Jan 23 '21 at 16:27
  • That makes no sense unless you also ask for the time of birth. – Andreas Jan 23 '21 at 16:31
  • 1
    @Andreas - Good point ...in fact, your comment regarding the time came almost at the same time when I replaced `ZonedDateTime` at start-of-day with the one-at-some-given-time. – Arvind Kumar Avinash Jan 23 '21 at 16:34
  • 1
    Well, at least I was born in a well-defined time zone. Without a time zone you can’t even establish a date of birth from the point in time the person was born. Agree that for most customer registration purposes we don’t need it (and often we don’t need the date either; in this assignment we do). – Ole V.V. Jan 25 '21 at 18:18
-2

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.

Petre Popescu
  • 1,950
  • 3
  • 24
  • 38
  • Yes I'd like to know that – Sye Jan 23 '21 at 16:21
  • 1
    Yeah, `new Date(915195600000l)` makes it really obvious what date is assigned. Why would you want the birth date to be at 8 AM? Oh, oops, you're in some other time zone. --- `YYYY, mm, dd` is definitely the wrong casing for the date format pattern. – Andreas Jan 23 '21 at 16:23
  • Yes, you are right, the casing was wrong. Corrected it. Regarding using the epoch-time, it was a habit on my side because working with the database usually I store the date as a timestamp. – Petre Popescu Jan 23 '21 at 16:26