10 Replies - 3020 Views - Last Post: 04 March 2012 - 11:36 AM Rate Topic: -----

#1 SaiLee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 19-February 12

Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 05:46 AM

Hi. I am trying to use the Date class for the very first time and I realised you can't assign a Date variable a String variable. So I guess my question is how do I assign a Date variable a date. My current code is below: Red line appears here no suitable constructor found for Reservation(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String,int,int)


    public Reservation() {
        this(0, "", "", "", "", "", 0, "", "", "", 0, 0);   
    }




The other constructor is:

  
public Reservation(int reservationID, String additionalRequirements, String isGourmet, String isThemed, String firstName, String surname, int gourmetID, String mobileNo, Date date, String reservationTime, int noOfTables, int partyPeople) {
        this.reservationID = reservationID;
        this.additionalRequirements = additionalRequirements;
        this.isGourmet = isGourmet;
        this.isThemed = isThemed;
        this.firstName = firstName;
        this.surname = surname;
        this.gourmetID = gourmetID;
        this.mobileNo = mobileNo;
        this.date = date;
        this.reservationTime = reservationTime;
        this.noOfTables = noOfTables;
        this.partyPeople = partyPeople;
    }


I don't have any experience with the Date class and its not recommended that we store dates as Strings as its frowned upon and not very professional

Is This A Good Question/Topic? 0
  • +

Replies To: Java.util.Date and How to Use in Right Way

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 05:49 AM

Have you considered using (or can you use) the Calendar class? If you're able to use that, I recommend it over Date.

As to how to assign a Date, investigate the Date() constructors at the above link and use whichever works well for you.

Edit: I wasn't done.

This post has been edited by GregBrannon: 04 March 2012 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 SaiLee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 19-February 12

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 05:56 AM

Hi. We have a Database which stores Dates in the Date format, so when we use ResultSets to get the dates, we use the rs.getDate() method. I am not familiar with the Calendar class. From reading the Date documentation and looking at the constructors, I am still confused of how to actually give a date variable an actual date so I can use it and get that red line to go away. thanks
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 06:04 AM

I would be using the SimpleDateFormat class for this task :) When you parse a String using the formatter, it actually uses the Calendar class. You simply apply a pattern to the SimpleDateFormat class, and then you parse a String after the given pattern, which will then give you a Date object.
Read the documentation for the SimpleDateFormat in the api!!!!! It also tells you how to use patterns.

String myDateAsString = "2012-05-10";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //Example: 2012-03-04
Date date = formatter.parse(myDateAsString, 0);

if (date == null)
	System.out.println("Could not parse the following date: " + myDateAsString);
else
	System.out.println("Parsed date to: " + date);


Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 06:06 AM

Show your Reservation class - at least the first part through the first constructor you've already shown.

As for assigning a date to a Date object, the API page I showed you is pretty clear. Do you not know how to read it? It's a skill you need, so if that's the problem let us know, and we'll help.
Was This Post Helpful? 0
  • +
  • -

#6 SaiLee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 19-February 12

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 06:12 AM

There really isn't much to see there. I have getters and setters for Date. Its currently a String but i have to change it to date and really don't know yet how to do this. SimpleDateFomat looks good but I'm not sure where your suggested code goes.

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 06:22 AM

If you're using a db you'll find it easiest with java.sql.Date:

new Reservation(...,Date.valueOf("2012-03-04"),

Was This Post Helpful? 0
  • +
  • -

#8 SaiLee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 19-February 12

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 06:35 AM

This helps thanks. In my reservation dialog form I need to get the date out of a text box so I use the getDate() method but it says:

required: java.sql.Date
found: java.util.Date
reason: actual argument java.util.Date cannot be converted to java.sql.Date by method invocation conversion

reservation.setDate(txtDate.getDate());  



Also following from your reply I tried to use the System Date in the constructor but I get this error: cannot reference this before supertype constructor has been called

 
 public Reservation() {
        this(0, "", "", "", "", "", 0, "", Date.valueOf(GetDateNow()), "", 0, 0);
    }




   
public String GetDateNow() {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy");
        String dateNow = formatter.format(currentDate.getTime());
        System.out.println(dateNow);
        return dateNow;
    }
 


Thanks kindly for your help
Was This Post Helpful? 0
  • +
  • -

#9 SaiLee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 19-February 12

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 07:47 AM

To fix the first error this seems to help:

reservation.setDate(Date.valueOf(txtDate.getDate() + ""));


Unfortunatley when I try saving this is the output:

java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Date.java:138)
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 08:25 AM

Quote

reservation.setDate(txtDate.getDate());  


Try changing that to

reservation.setDate(new java.sql.Date(txtDate.getDate().getTime()));  

Was This Post Helpful? 0
  • +
  • -

#11 SaiLee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 19-February 12

Re: Java.util.Date and How to Use in Right Way

Posted 04 March 2012 - 11:36 AM

Fixed it like this.

txtDate.setDate(reservation.getDate());


Just realised that when I click edit reservation, the correct date for that reservation (from db) appears in the text field correctly.

When I click to add a new reservation (uses same dialog box) it throws an error - InvalidArgumentException. I think this makes sense because there is no date in that box when you click new.

This line throws the InvalidArgumentException. This is of course the constructor used to add a reservation, I'm quite stuck with this date issue now.
public Reservation() {
        this(0, "", "", "", "", "", 0, "", Date.valueOf("Jan 1, 2012"), "", 0, 0);
    }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1