File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes sql.Date problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "sql.Date problem" Watch "sql.Date problem" New topic

sql.Date problem

padma patil
Ranch Hand

Joined: Nov 06, 2001
Posts: 41
Folowing is my senario,
1) I am reading Form input text field 'Date' as
String str1=request.getParameter("Date");
which accepts input in yyyymmdd format.
eg: str1="20020422"
2) I want to store this Date into table with column type Date
AssignedOn Date
3)I am using PrepareStatement to insert values into table.
problem here is Datevar must be sql.Date type
4)I am converting String to sql Date as
Long l1=new Long(str1);
long l2=l1.longValue();
Date d1= new Date(l2);
System.out.println("long value:"+ l2);
System.out.println("Date:"+ d1.toString());
First print prints:long value:4122002
Second prints : Date:1969-12-31
Why it is taking wired value?
I even try by giving String in 'mmddyyyy' format
it prints 1969-12-31.
what's happening here?
Please let me any one knows any other way to store Date value(which is from htnl form) in table.
[ April 22, 2002: Message edited by: padma patil ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
The constructor for Date that takes a long as an argument creates a date based on the number of milliseconds since January 1 1970 at 00:00:00 not the date itself.
To do what you are trying to do why ot just take the date you get and add the dashes (-) into it separate the yyyy-mm-dd and then just store it that way?

I agree. Here's the link:
subject: sql.Date problem
It's not a secret anymore!