1

I would like to know how to use type Date in new object?

I'll explain a bit more be showing my code:

I got a Java class called:

Reiziger:

import java.util.Date;

public class Reiziger {
    private String naam;
    private Date gbdatum;

    public Reiziger(String nm, Date gbdtm) {
        this.naam = nm;
        this. gbdatum = gbdtm;
    }

    public String getNaam() {
        return naam;
    }

    public void setNaam(String naam) {
        this.naam = naam;
    }

    public Date getGBdatum() {
        return gbdatum;
    }

    public void setGBdatum(Date gbdatum) {
        this.gbdatum = gbdatum;
    }

    public String toString() {
        return naam + gbdatum;
    }
}

And a Main:

public class Main {
    public static void main(String[] args) {
        Reiziger r1 = new Reiziger("Joost", "01-01-1994");
        Reiziger r2 = new Reiziger("Hans", null);

        ReizigerOracleDaoImpl i1 = new ReizigerOracleDaoImpl();
        i1.alleReizigers.add(r1);
        i1.alleReizigers.add(r2);
        System.out.println(i1.alleReizigers);
    }
}

As you see in my main i tried alot of things only null is working. I want to add the the object a date -> for example: 01-01-1994 but doesnt work.

I tried it without the " " also nothing.

any idea what i'm doing wrong?

Ascalonian
  • 14,409
  • 18
  • 71
  • 103
Plox333
  • 61
  • 2
  • 9

5 Answers5

2

Your method is defined as taking in a Date object, however you are trying to pass in a String. Java does not automatically convert the String to a Date.

What you need to do is use a DateFormatter.

This uses a defined Date pattern to convert your incoming String into the Date object you want to use.

In your case, you would want to do something similar to this:

String dateFormat = "MM-dd-yyyy";

try {
    DateFormat df = new SimpleDateFormat(dateFormat);
    Date dateToUse = df.parse("01-01-1994");

    // Or this way
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
    LocalDate localDate = LocalDate.parse("01-01-1994", formatter);
} catch (ParseException pe) {
    // Do something
}

Here are the links to the Java PI:
SimpleDateFormatter: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

DateTimeFormatter: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

LocalDate: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

Ascalonian
  • 14,409
  • 18
  • 71
  • 103
  • 1
    I made the format follow "MM-dd-yyyy" since countries outside of the US follow this format. Obviously if I have the day/month switched, just change them in there. – Ascalonian Apr 23 '18 at 19:54
2

tl;dr

You are passing a String object to your constructor, but then attempting to assign it to a member variable of a different type (Date). No can do. Square peg, round hole. You must first parse that text into an object of the correct type.

Another problem: You are using terrible old date-time class Date where you should be using the modern java.time.LocalDate class.

LocalDate.of( 1994 , Month.JANUARY , 1 )

java.time

I would like to know how to use type Date in new object?

Don’t.

Apparently you want a date-only value, so use a date-only class rather than a date-plus-time-of-day class.

Furthermore, never use java.util.Date. It is part of the troublesome old date-time classes that were supplanted years ago by the java.time classes.

The LocalDate class represents a date-only value without time-of-day and without time zone. Specify the month part either by number 1-12 for January-December or by the Month enum class.

LocalDate ld = LocalDate.of( 1994 , Month.JANUARY , 1 ) ;

Change your member variable:

private Date gbdatum ;

…to:

private LocalDate gbdatum ;

Change your constructor to:

public Reiziger(String nm, LocalDate gbdtm)

Call it like this:

Reiziger r1 = new Reiziger( 
    "Joost" , 
    LocalDate.of( 1994 , Month.JANUARY , 1 ) 
) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

https://docs.oracle.com/javase/7/docs/api/java/util/Date.html

Date(String) is Deprecated. Use Date(Long) After that use a SimpleDateFormatter new SimpleDateFormatter ("dd-MM-yyyy") ;

Wulf
  • 712
  • 5
  • 25
0

"01-01-1994" creates a String with those contents. To create a Date you will need to use one of it's constructors. For example, using the default constructor

Reiziger r1 = new Reiziger("Joost", new Date());

will work.

For more information on the class along with it's other constructors see the official documentation.

However the Date class is a bit old at this point, and as you see from other answers it's a bit of a mess to work with properly, instead I would recommend using LocalDate from the java.time package. If you are using that class then the following will create a date representing "01-01-1994": LocalDate.of(1994, 1, 1)

Diasiare
  • 745
  • 1
  • 6
  • 18
0

Personally I would recommend using classes from the more recent java.time library but if you want to use java.util.Date and its subclasses and set the date via a String then you can use a SimpleDateFormat like this:

private final static DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
 // ...
public static void main(String[] args)  {
    // do exception handling for `parse`
    Reiziger r1 = new Reiziger("Joost", dateFormat.parse("01-01-1994"));
    Reiziger r2 = new Reiziger("Hans", null);
    // ...
}
xtratic
  • 4,600
  • 2
  • 14
  • 32