473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to convert String to java.sql.Date()?

Dear All,

I have one doubt for how to insert the java.sql.Date into ms-sql server.
I tried the below code but it couldn't be inserted. I did wrong in
my code that is ps.setDate(1,full_date_time) to ps.setString(1,full_date_time);
How can i covert the String(full_date_time) to java.sql.Date() and insert the MM/DD/YYYY format in ms-sql database?
I set datetime as datatype in my products table.

<%
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
Date d=new Date();

String full_date_time=sdf.format(d).trim();
out.println("Date--------"+full_date_time);
ps = con.prepareStatement("insert into products values(?)");
ps.setDate(1,product_id);
ps.executeUpdate();
%>

thanks in advance


V. Prasath.
Jun 22 '07 #1
14 82109
r035198x
13,262 8TB
Dear All,

I have one doubt for how to insert the java.sql.Date into ms-sql server.
I tried the below code but it couldn't be inserted. I did wrong in
my code that is ps.setDate(1,full_date_time) to ps.setString(1,full_date_time);
How can i covert the String(full_date_time) to java.sql.Date() and insert the MM/DD/YYYY format in ms-sql database?
I set datetime as datatype in my products table.

<%
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
Date d=new Date();

String full_date_time=sdf.format(d).trim();
out.println("Date--------"+full_date_time);
ps = con.prepareStatement("insert into products values(?)");
ps.setInt(1,product_id);
ps.executeUpdate();
%>

thanks in advance


V. Prasath.
What error message did you get ?
Jun 22 '07 #2
What error message did you get ?
I wrongly typed in ps.setDate(1,date_of_insert); instead of
ps.setInt(1,date_of_insert);
so please pardon me.


when i compile that coding it throws the followin error......

Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(1,date_of_insert);
Jun 22 '07 #3
r035198x
13,262 8TB
I wrongly typed in ps.setDate(1,date_of_insert); instead of
ps.setInt(1,date_of_insert);
so please pardon me.


when i compile that coding it throws the followin error......

Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(1,date_of_insert);
To convert the String to sql Date, you use Date.valueOf (string). The string must be in the format yyyy-mm-dd
Jun 22 '07 #4
sumittyagi
202 Expert 100+
I wrongly typed in ps.setDate(1,date_of_insert); instead of
ps.setInt(1,date_of_insert);
so please pardon me.


when i compile that coding it throws the followin error......

Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(1,date_of_insert);
at one hand you are saying you want to insert date(and you are using the correct method as well), on the other hand you are passing the string object to it. Pass date object only. You don't have to worry about the database date format for that, setDate function will be taking care of that.
Jun 22 '07 #5
r035198x
13,262 8TB
at one hand you are saying you want to insert date(and you are using the correct method as well), on the other hand you are passing the string object to it. Pass date object only. You don't have to worry about the database date format for that, setDate function will be taking care of that.
Yes, there's no need to change the date to string in the first place.
Jun 22 '07 #6
To convert the String to sql Date, you use Date.valueOf (string). The string must be in the format yyyy-mm-dd
thanks for reply..

i tried the following code to ur suggestion but it shows the
the following error: java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Date.java:104)

java.sql.Date dt = java.sql.Date.valueOf(full_date_time);
Jun 22 '07 #7
r035198x
13,262 8TB
thanks for reply..

i tried the following code to ur suggestion but it shows the
the following error: java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Date.java:104)

java.sql.Date dt = java.sql.Date.valueOf(full_date_time);
Look at my first reply again, it requires the date to be in yyyy-mm-dd format. Better still don't change the date to the String at all. Just set it as a Date objec without formating it with the date format.
Jun 22 '07 #8
sumittyagi
202 Expert 100+
thanks for reply..

i tried the following code to ur suggestion but it shows the
the following error: java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Date.java:104)

java.sql.Date dt = java.sql.Date.valueOf(full_date_time);
I am not able to understand what u are trying to do.
you first converted date to string.
then again converting string to date.
what for?

and second thing, you can't use valueOf(String) function here (I havn't heard of this function in date class, but it might exist for any other purpose) because, how will the date class be knowing what format your date string is in. there are thousands of formats of a date string.
You can get your date object back from string with SimpleDateFormat object only, by using the parse(String) function.
and if the date string is not in the same format as of SimpleDateFormat object then it will throw some parse Exception(I don't remember the exact exception).

But I don't understand why u need to convert date to string in the first place.
Jun 22 '07 #9
Prasath,

did u get solution for this problem??? If so, please let me know...

Thanks
Nov 20 '07 #10
r035198x
13,262 8TB
Prasath,

did u get solution for this problem??? If so, please let me know...

Thanks
Did you read the replies in this thread?
What problem are you getting?
Nov 20 '07 #11
Did you read the replies in this thread?
What problem are you getting?
I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.
Nov 20 '07 #12
r035198x
13,262 8TB
I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.
Have a look at Oracle's TO_DATE function.
Nov 20 '07 #13
heat84
118 100+
I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.
Will java.sql.Date be the accurate date since you want the date up to the second. Doesn't it give only 00/00/00 for the hours , minutes and seconds. I think java.util.Date is more accurate.
Nov 20 '07 #14
r035198x
13,262 8TB
Will java.sql.Date be the accurate date since you want the date up to the second. Doesn't it give only 00/00/00 for the hours , minutes and seconds. I think java.util.Date is more accurate.
Why not use java.sql.Timestamp?
Nov 20 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
1
by: Num | last post by:
Hi all, I have to convert a J2EE date as a long ("Millis") in a .NET date as a long ("Ticks") In Java, currentTimeMillis, is the difference, measured in milliseconds, between the current time...
2
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings...
12
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
4
by: sang | last post by:
How can I convert a string like '11-oct-2006' into a valid mysql date? the date_format doesnot change the string in requried date format. create table sample(name varchar(20),date varchar(16));...
3
by: Lonifasiko | last post by:
Hi, I want to convert an UTC time to a Date object in Javascript. The UTC time we have is a string that looks like "1160720058.377452373" for example. I've done it in Java but I'm not able...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.