Error after trying to convert Date into String
I've been trying so hard to convert the date into a string and this is the closest I've gotten i think.
Code:
SpeedDating thanksgiving = new SpeedDating() ;
input = JOptionPane.showInputDialog
("Enter the year for november ") ;
int year = Integer.parseInt(input) ;
java.util.Date dateNow = new java.util.Date();
SimpleDateFormat formatDateJava = new SimpleDateFormat("dd/MM/yyyy");
String date_to_string = formatDateJava.format(thanksgiving.getThanksgiving(year));
System.out.println("Today's date into dd/MM/yyyy format: " + date_to_string);
The error shows up after running the program. It says, "Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date". What's going on? How can I fix it?
Re: Error after trying to convert Date into String
What is the full text of the error message that shows the line number where the error happens?
What does the getThanksgiving() method return?
What type of argument does the format method take?
Re: Error after trying to convert Date into String
I'm sorry. It supposed to return a date in the form (month, day, year). The year is inputted by the user.
Code:
Date Thanksgiving = new Date(10, 1, year);
int count = 1 ;
int weekCount = 0;
for ( ; count <= 30; count++)
{
if(Thanksgiving.getDay()==4) {
weekCount++;
}
if (weekCount == 4) {
break;
}
Thanksgiving=new Date(10, count, year);
The format method? You mean the "formatDateJava.format" part? I'm trying to print out the date.
When I had this
Code:
SpeedDating thanksgiving = new SpeedDating() ;
input = JOptionPane.showInputDialog
("Enter the year for november ") ;
int year = Integer.parseInt(input) ;
System.out.println(thanksgiving.getThanksgiving(year)) ;
This prints out something like Date@1762027 which I was told was a reference "memory location". I need to convert that into a string.
Re: Error after trying to convert Date into String
I asked a similar question to this in a previous post. I was told to use simpledateformat but I I don't know :(
Re: Error after trying to convert Date into String
Did you see the questions I asked in my post? Can you answer them?
What does the getThanksgiving() method return? What type of data does the method return?
What type of argument does the format() method take? Read the API doc to see for sure.
That looks like what the toString() method would return for the class: Date.
Do you have your own version of the Date class? If so, add a toString() method to it that returns the contents of the class in a String in a useful format.
If you have your own class named Date, that is a problem because Java SE has a class with the same name and that will be confusing for anyone reading your code. I recommend changing the class's name.
I'm done for tonight. Back tomorrow. Some one else could come after this.
Re: Error after trying to convert Date into String
Thank you. d1.getShortDate() // returns the date as mm/dd/yyyy
How would I use that? So something like System.out.println(thanksgiving.getShortDate()); But I need to access the date that is stored in Thanksgiving which is in the other class. How would I do that?
Re: Error after trying to convert Date into String
Quote:
How would I use that? So something like System.out.println(thanksgiving.getShortDate());
What happens when you try that in the code?
To access data in another class, you can call a method in that class and have it return the data.
Is this related?
http://www.javaprogrammingforums.com...y-methods.html