-
conversion of string into java.sql.Date
hi,
i have a string in this format:-
String s = year+"-"+e_month+"-"+date;
and i want to covert it into
java.sql.Date sqlToday ;
so that i can save my values in the data base
i did every thing realted to that like:-
SimpleDateFormat ts= new SimpleDateFormat("yyyy-MM-dd");
sqlToday = new java.sql.Date(ts.parse(s).getTime());
but couldnt get the results
so any one can help me
thanks
Last edited by vikassheelgupta; 08-16-2005 at 06:01 AM.
Reason: rectify one errror that i can think once again
-
 Originally Posted by vikassheelgupta
hi,
i have a string in this format:-
String s = date+"-"+e_month+"-"+year;
and i want to covert it into
java.sql.Date sqlToday ;
so that i can save my values in the data base
i did every thing realted to that like:-
SimpleDateFormat ts= new SimpleDateFormat("yyyy-MM-dd");
sqlToday = new java.sql.Date(ts.parse(s).getTime());
but couldnt get the results
so any one can help me
thanks
Your format appears backwards from the way you say your string is.
Isn't your string Day-Month-Year? So "dd-MM-yyyy".
-
Hi
i have changed it but the result will be the same
even i change it before u can tell me
thanks alot
-
 Originally Posted by vikassheelgupta
Hi
i have changed it but the result will be the same
even i change it before u can tell me
thanks alot
Well SimpleDateFormat certainly works so the problem is with your code.
What do you mean it doesn't work?
There is an error? The date is wrong?
Please give more details. If you are getting an error then please post the error message.
-
hi
sorry it didnt work
i can able to change the format but it is in java.util.Date not in java.sql.Date
and one more thing thats my code:-
String s = date+"-"+e_month+"-"+year;
SimpleDateFormat ts= new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat ts1= new SimpleDateFormat("dd-MMM-yyyy");
if(p!=null)
{
//sqlToday = ts.parse( s );
sqlToday = ts.parse( s );
}
System.out.println( ts1.format( sqlToday )) ;
i am unable to store this value in any variable of type java.util.Date or java .sql.Date
i cant store
so how can i store it
thanks alot
-
I don't think the conversion is any problem:
Code:
static Calendar cal=Calendar.getInstance();
// handles the format yyyy-mm-dd
private java.sql.Date getDate(String dateString) throws Exception {
String [] sd=dateString.split("-");
int year=Integer.parseInt(sd[0].trim());
int month=Integer.parseInt(sd[1].trim());
int date=Integer.parseInt(sd[2].trim());
cal.set(year,month,date);
java.sql.Date sD=new java.sql.Date(cal.getTimeInMillis());
return sD;
}
Last edited by sjalle; 08-18-2005 at 06:00 AM.
eschew obfuscation
-
With all due respect, sjalle, I think you'll find your code produces unexpected results. Calendar months are zero-indexed, so the resulting month of your Date object will be off-by-one. For example, if you use "2005-08-18" as your string, the resulting date from your method will show September as the month (Calendar.SEPTEMBER == 8).
It's unclear what you're trying to do. Does the following help?
Code:
String s = "18-AUG-2005";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
java.sql.Date sqlToday = new java.sql.Date(df.parse(s).getTime());
System.out.println(sqlToday); // 2005-08-18
Formatting a Date Using a Custom Format
Parsing a Date Using a Custom Format
Out of curiosity, why are you trying to parse a SQL date from a String? If you need to insert the date into a database, simply use a PreparedStatement and setDate(). I can't really see why else you might be trying to get a java.sql.Date as opposed to just a java.util.Date...
Last edited by yawmark; 08-18-2005 at 08:35 AM.
-
I forgot about that one, sorry.
Zero-based months is not a new invention, but, add 1 to month and its ok.
I assumed a numeric month.
The database problem is mentioned in the first post in this thread.
eschew obfuscation
-
I forgot about that one, sorry.
Zero-based months is not a new invention, but, add 1 to month and its ok.
I assumed a numeric month.
Adding one is an easy fix, but I'd still recommend parsing the date using DateFormat. If one *does* need to address the individual months, I'd say stick to the Calendar constants (e.g., Calendar.JULY, etc.). These kinds of OBO errors have a tendency to creep into code and be tough to root out.
The database problem is mentioned in the first post in this thread.
Yeah, I'm still not sure of what the actual issue is, as "not working" leaves a lot of possibilities...
-
I agree that the format parse is the best solution, especially when you know what
kind of date formats you are dealing with, and Calendar is the best class for
processing dates, yes.
My inclination to do a string parse comes from my experience with loading data from
multiple sources into a content management system I made a few years back. I then
had to make a date sniffer due to the insane variety of date "formats" that people like to use.
Since it is a database entry that doesn't work for a date column I think we can
assume a plain SQLException due to an invalid date.....
I don't know what OBO errors are though.
eschew obfuscation
-
My inclination to do a string parse comes from my experience with loading data from
multiple sources into a content management system I made a few years back. I then
had to make a date sniffer due to the insane variety of date "formats" that people like to use.
LOL. I hear you. And to be true, if the purpose is simply to transform one date string to another format, parsing a Date object can be overkill.
Since it is a database entry that doesn't work for a date column I think we can assume a plain SQLException due to an invalid date.....
Agreed. It would be nice to know exactly what was wrong with the date; i.e., what was invalid about it.
I don't know what OBO errors are though.
Sorry - "Off-By-One", aka "fencepost" errors.
Regards...
Similar Threads
-
By mdengler in forum ASP.NET
Replies: 0
Last Post: 11-26-2002, 02:32 PM
-
By Fred Mayes in forum Java
Replies: 1
Last Post: 06-05-2001, 06:12 AM
-
By Julian Milano in forum VB Classic
Replies: 0
Last Post: 08-10-2000, 09:16 PM
-
By Chandra in forum VB Classic
Replies: 0
Last Post: 06-22-2000, 07:30 AM
-
By Robert Rieth in forum VB Classic
Replies: 1
Last Post: 04-11-2000, 03:21 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|