Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion of string to date

 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i'm trying to develop a module which reads the date from the Web page as inputed by the user and stores the date in the database.

My JSP page is as follows:


and the java file is as follows:



now when i'm trying to compile in the using ANT its giving the follwowing error:



please help
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parse method has two overloaded forms

parse(String) and parse(String source, ParsePosition pos)

but your code seems to be passing a Date object.
 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the JSP page is sending the input as String only.
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be. But look at your method call

 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the changes and removed the parser from the statement where the method returns the value but it is still giving the same error for the one parser in the setDate method
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i made the changes, now the code is:



and the error during compiling is:
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the error
java.text.ParseException; must be caught or declared to be thrown

so you should either throw the exception or catch it
 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks... now the code gets compiled and is running..
but not according hoe it should..
the code is now..


its sending the date as "null"

I checked the logs and its something like this:
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the use of following line?
private Date dates=null;
 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its used for intializing and local declaration of the variable "dates"
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try one thing, put two print statements in this method to make sure you are getting the values properly.



and in

 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did the additions suggested by you. Now the log is:



The print statements in the setDate() weren't even executed.. so I think there might be a problem with that.
I even tried changing

public void setDate(String value)

to

void setDate(String value)

but that also gave the same result
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this once
in your execute method


make that dates variable as String


and change the definition of setDate and getDate as follows
 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Made the changes suggest by you. The code is now



and while compiling its giving an error:
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


should be
 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its giving an error "incompatible types"


so i made a change and converted


to


it compiled properly but while running struts gave an error:
 
Mai Vala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
format method takes date datatype as method parameter and returns String...
 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the code from



to


and it compiled and was able to execute.. but the "null" problem is still there.
the logs are:
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is small mistake in the code that Mai has pointed out, you need to call parse not format. However this does not seem to be related to the null issue.
 
Deepti Gupta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed format to parse.... Only after making the change the file is able to be executed....
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is setter method for date is not getting called.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic