Issue with converting a string to a date
I have this section:
Code :
public void set_doh(int new_m, int new_d, int new_y)
{
String datestring = Integer.toString(new_m) + "/" + Integer.toString(new_d) + "/" + Integer.toString(new_y);
System.out.println(datestring);
DateFormat DOB = new SimpleDateFormat("m/d/yyyy");
try
{
Date convertedDate = DOB.parse(datestring);
this.doh = convertedDate;
}
catch (ParseException e) {
System.out.println("Failed to parse");
}
}
which is passed information from here:
Code :
employee1.set_doh( Integer.parseInt(scanner.nextLine()),Integer.parseInt(scanner.nextLine()),Integer.parseInt(scanner.nextLine()) ); // int
The datestring is printed in the correct format in the first block but when it goes to convert it into Date format I get:
Code :
E:\Java Project\Employee.java:145: error: incompatible types
Date convertedDate = DOB.parse(datestring);
^
required: Date
found: java.util.Date
1 error
Now, I have a Date.java class in the same folder which was given to me by my instructor and I'm supposed to use that in conjunction with a Dateutil.java she gave me as well. I've noticed that if I remove Date.java from the folder this section of the code will work but then I cannot use the Dateutil.java properly, which is necessary to compare the current date with the object's date.
Should I post the all the code in all three files?
Thanks for any and all help on this!
Re: Issue with converting a string to a date
Try putting the full package path on the class name so the compiler does not get confused about class definitions: java.util.Date
Which Date class is convertedDate supposed to be?
Re: Issue with converting a string to a date
Hi Norm,
I actually tried that but there are other lines which compare convertedDate to objects represented in the Date class, so they have to be compatible at some point.
The program is supposed to take the date as the three separate integers from the text file and set them into the employee object. However, they are meant to be made into a proper Date format because I am then supposed to use the date to calculate the employee's years of service.
So to answer your question convertedDate is supposed to be the Date class. The program I assume needs to convert from java.util.Date to Date, or perhaps I need to write the parse() method into the Date class I was provided? Do you know how I could do that?
Re: Issue with converting a string to a date
Quote:
convertedDate is supposed to be the Date class.
If convertedDate is the local Date class, then you can not assign it the value returned by the parse() method which returns a java.util.Date object.
Quote:
The program I assume needs to convert from java.util.Date to Date
Yes, that seems right.
What methods does the java.util.Date class have that can be used to create a local Date class object?
Re: Issue with converting a string to a date
Quote:
Originally Posted by
Norm
What methods does the java.util.Date class have that can be used to create a local Date class object?
I feel like I should be the one asking this question!
Re: Issue with converting a string to a date
I don't have a definition for the local Date class. You have to read the code or the API doc for that class to see how to use it.
How can anyone make recommendations for using an unknown class?
Re: Issue with converting a string to a date
This is all it contains:
Code :
public class Date
{
private int month;
private int day;
private int year;
Date()
{
}
Date(Date aDate)
{
month = aDate.month;
day = aDate.day;
year = aDate.year;
}
Date(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
public int get_month()
{
return month;
}
public void set_month(int new_month)
{
month = new_month;
}
public int get_day()
{
return day;
}
public void set_day(int new_day)
{
day = new_day;
}
public int get_year()
{
return year;
}
public void set_year(int new_year)
{
year = new_year;
}
} // end Date class
Re: Issue with converting a string to a date
Looks like you need to create a Calendar object, set its time using the Date object, then call the Calendar object's get method to get the year, month and day to create the local Date object.
Re: Issue with converting a string to a date
Code :
java.util.Date convertedDate = DOB.parse(datestring);
Calendar calendar = Calendar.getInstance();
calendar.setTime(convertedDate);
this.doh = calendar.getTime();
Same error.
Re: Issue with converting a string to a date
There are two ways you can do it with relative ease.
One way that *should* work, but may not be what your instructor prefers you to do is by saying:
Code java:
...
java.util.Date convertedDate = DOB.parse(datestring);
Date dateObject = new Date(...); /*send the arguments for the month, day, and year by calling the convertedDate.getYear(), ect. methods. NOTE: this will draw a handful of warnings since the java.util.Date class has been more or less Deprecated (Calendar is the more widely used one now)*/
...
Alternatively, you can do what I expect your instructor to *want* you to do, which would be just using the values sent to the set_doh(int,int,int) method. Why are you even creating the datestring and parsing the values back out? Why not just use the int values you used to create the datestring?
Re: Issue with converting a string to a date
What was the text of the error?
What datatype is doh? Does the Calendar class's getTime() method return that type of data?
You need to use the Calendar class's get() method to get the year, month and day to use with set method on the local Date class object. See the Date class's getMonth() method API doc for how to code the get method.
Re: Issue with converting a string to a date
aussiemcgr,
I actually added the
Code :
Date(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
to the Date class, it wasn't there before.
I've tried your alternate suggestion previously and when I print the dates back they come out as "Date@kdfj23" or some such unusable muck. Which is why I have been trying to find another way to do it.
I've just realized that maybe I simply need to convert that "Date@kdfj23" back into a more presentable form.
Is this the case?
Re: Issue with converting a string to a date
Norm,
The text of the error is the same as originally,
required: Date
found: java.util.Date
doh is "Date"
Re: Issue with converting a string to a date
Quote:
come out as "Date@kdfj23" o
That is what is returned by the class's default toString() method. You should override the toString method and have it return the String you want to see.
Re: Issue with converting a string to a date
Quote:
required: Date
found: java.util.Date
See post#11.
Re: Issue with converting a string to a date
Norm, aussiemcgr,
Thanks for the help, especially the tip about toString! #:-s
I should have mentioned the string the program was giving me in the OP, would have saved us some time haha.
Dave