-1

In my Java class, I imported the package java.util.Date and created a variable private Date birthdate;

My class constructor takes in a first name, last name, and birthdate. I have tried passing the date as a String but it doesn't work:

Person per = new Person("Angelina","Jolie","1975/06/04");

How do I correctly pass a Date object to my constructor?

Marsroverr
  • 556
  • 1
  • 6
  • 23
  • 3
    I recommend right away using Java 8 dates, such as LocalDate. Those old Date libraries are just for backwards compatibility. If you're learning Dates from scratch anyway, might as well start with the correct ones. – Lotsa May 20 '20 at 17:51
  • I am just a beginner (I have a project for my uni and the teacher told us to use java.util.Date). I don't know what scratch is! Thank you for your answer but I must use java.util.Date... – Alexia Achthoforidou May 20 '20 at 17:55
  • "from scratch" means "from the beginning" / "from zero". – Slaw May 20 '20 at 17:55
  • And the key to working with dates, the concept, is you have to use correct formatting, of which there are many possibilities." yyyy-MM-dd" is the default in my Locale. Are you sure format "1975/06/04" is how your Date expects to be formatted? You use a date formatter if you want to handle different ways of expressing or inputting a date object. yyyy-MM-dd or dd MMM yyyy ... etc, etc, etc. – Lotsa May 20 '20 at 17:56
  • ok (for scratch)! yes the date format must be yyyy-MM-dd. So I must write Person per = new Person("Angelina","Jolie",1975-06-04); ??? – Alexia Achthoforidou May 20 '20 at 17:58
  • 4
    Does this answer your question? [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Savior May 20 '20 at 17:58
  • Would probably be passed as a String. Person per = new Person("Angelina","Jolie","1975-06-04"); – Lotsa May 20 '20 at 17:59
  • 2
    Teachers who tell their students to use the poorly designed and long outdated `java.util.Date` class deserve to be sacked. Για σου and sorry to bring you this bad news. – Ole V.V. May 20 '20 at 18:06
  • 2
    Teachers, sacked! :D I just returned to Jave after many years away, and spent a few days last week converting my brand new project from old Date libraries and longs ... to all Date work done in Jave 8. Ya, why use an old abandoned thing that's been updated for you. I like the new library so far. :) – Lotsa May 20 '20 at 18:09

2 Answers2

2

To create your date use something like - LocalDate date = LocalDate.of(1975, 06, 04); and pass it to your parametrized constructor. or you can use

public static Date parseDate(String date) {
 try {
     return new SimpleDateFormat("yyyy-MM-dd").parse(date);
 } catch (ParseException e) {
     return null;
 }
}

This will return you your date and you can pass it to your constructor.

One more approach for this -

 Date date = new GregorianCalendar(1975, Calendar.APRIL, 06).getTime();

Given the Java 8 APIs best way to do this is using LocalDate.

thealchemist
  • 421
  • 4
  • 12
2

java.time

It’s not the answer your teacher asked for. Given that it’s a requirement to pass an old-fashioned java.util-Date object to the constructor, I would do:

    LocalDate birthDate = LocalDate.of(1975, Month.JUNE, 4);
    Instant startOfDay = birthDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
    Person per = new Person("Angelina", "Jolie", Date.from(startOfDay ));

Part of the challenge is that despite the name a Date does not represent a date. It represents a moment in time (with millisecond precision). In that moment it’s a different date in different time zones of the Earth. The conventional solution to this dilemma is to take the first moment of the day (usually 00:00) in your own time zone. So your Date representing 4th June 1975 is different from my Date representing the same date. Oh horrors.

I am using java.time, the modern Java date and time API, as far as I can. In the end I do need to convert to a Date, of course. My first line of code defines the date of 4th June 1975 more clearly than any of the old date-time classes can. The second line finds the start of the day in the user’s time zone (more precisely, in the default time zone of the JVM where the program is running). Finally Date.from(startOfDay) converts to a Date.

Bonus points:

  • I find the code clear to read.
  • I have made the first step to prepare for the day when either the teacher realizes that there is a better way or we get a better teacher.

Edit: If it is also a requirement that the date be given as 1975-06-04, just define it in this way instead:

    LocalDate birthDate = LocalDate.parse("1975-06-04");

1975-06-04 is ISO 8601 format, the international standard. This is what LocalDate.parse() expects.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    I like that it will show the teacher there is a new way to do this that explicitly replaces what is considered an old outdated library. Hopefully they take note. One could add a comment in the code saying ... using modern libraries to revert back to legacy code ... to help politely reinforce the point. – Lotsa May 20 '20 at 18:34