473,651 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

30 New Member
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,fu ll_date_time) to ps.setString(1, full_date_time) ;
How can i covert the String(full_dat e_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.

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

String full_date_time= sdf.format(d).t rim();
out.println("Da te--------"+full_date_tim e);
ps = con.prepareStat ement("insert into products values(?)");
ps.setDate(1,pr oduct_id);
ps.executeUpdat e();
%>

thanks in advance


V. Prasath.
Jun 22 '07 #1
14 82171
r035198x
13,262 MVP
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,fu ll_date_time) to ps.setString(1, full_date_time) ;
How can i covert the String(full_dat e_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.

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

String full_date_time= sdf.format(d).t rim();
out.println("Da te--------"+full_date_tim e);
ps = con.prepareStat ement("insert into products values(?)");
ps.setInt(1,pro duct_id);
ps.executeUpdat e();
%>

thanks in advance


V. Prasath.
What error message did you get ?
Jun 22 '07 #2
prasath03
30 New Member
What error message did you get ?
I wrongly typed in ps.setDate(1,da te_of_insert); instead of
ps.setInt(1,dat e_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.Strin g to java.sql.Date.
ps.setDate(1,da te_of_insert);
Jun 22 '07 #3
r035198x
13,262 MVP
I wrongly typed in ps.setDate(1,da te_of_insert); instead of
ps.setInt(1,dat e_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.Strin g to java.sql.Date.
ps.setDate(1,da te_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 Recognized Expert New Member
I wrongly typed in ps.setDate(1,da te_of_insert); instead of
ps.setInt(1,dat e_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.Strin g to java.sql.Date.
ps.setDate(1,da te_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 MVP
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
prasath03
30 New Member
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.Illeg alArgumentExcep tion
at java.sql.Date.v alueOf(Date.jav a:104)

java.sql.Date dt = java.sql.Date.v alueOf(full_dat e_time);
Jun 22 '07 #7
r035198x
13,262 MVP
thanks for reply..

i tried the following code to ur suggestion but it shows the
the following error: java.lang.Illeg alArgumentExcep tion
at java.sql.Date.v alueOf(Date.jav a:104)

java.sql.Date dt = java.sql.Date.v alueOf(full_dat e_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 Recognized Expert New Member
thanks for reply..

i tried the following code to ur suggestion but it shows the
the following error: java.lang.Illeg alArgumentExcep tion
at java.sql.Date.v alueOf(Date.jav a:104)

java.sql.Date dt = java.sql.Date.v alueOf(full_dat e_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 SimpleDateForma t object only, by using the parse(String) function.
and if the date string is not in the same format as of SimpleDateForma t 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
Velmurugan1982
2 New Member
Prasath,

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

Thanks
Nov 20 '07 #10

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

Similar topics

2
39797
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 date variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown below. Any suggestions? code: String sailYear = (String)sail_date.toString().substring(0,4);
1
3103
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 Transformer convert that via XSLT to valid XSL-FO. So I define a SAXReader which fires the SAX Events for the Java Object. This works fine and the Transformation to PDF is ok. However, I have one object which contains an XHTML String and the tags
1
3613
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 and midnight, January 1, 1970 UTC. In .NET, DateTime.Ticks is the 100-nanosecond intervals that have
2
5081
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 set to "en-UK" and date to
12
3843
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
71398
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
82925
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)); insert into sample values('java','11-oct-2006'); but at the time of select statement convert the string into date to change the date format like 11-10-2006. so please help me to solve my problem. i am using the dos prompt not in php.
3
17493
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 to do it via Javascript, that should be pretty similar...
0
10751
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.