This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string to date conversion

 
Krish Khan
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to upload data from excel sheet using servlets. I can do that but problem is

I am reading date from excel and storing in string its in dd/mm/yyyy h:mm formate now i try to insert into MS Sql DB its show error conversion failed while converting string to Datetime.

Pleas let me know how to convert Stirng to date time format I want in dd/mm/yyyy h:mm:ss

Thank you!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this has nothing to do with Servlets, it's been moved to a more appropriate location.
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy h:mm");
Date date = format.parse("25/10/2010 1:09");


For more information on the argument for SimpleDateFormat see the link
SimpleDateFormat
 
Krish Khan
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chiru ,

Thanks for your reply . I have used the same but i coudnt able to manage. Here is my code and output.


out put is :

line number:: 7
date note :: 22/09/2010 10:31
Date::Fri Jan 22 10:31:00 SGT 2010
line number:: 8
date note :: 22/09/2010 10:32
Date::Fri Jan 22 10:32:00 SGT 2010


i am getting date as Jan 22 10:32:00 SGT 2010

I want it as 22/01/2010 10:32:00


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Date only has a format for debugging purposes. You can use the DateFormat to get it in format that you want.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, there is an error in line 7 of your code:

This should have been:

Note that you must use "MM" for the month, not "mm", because "mm" means minutes.

Krish Khan wrote:i am getting date as Jan 22 10:32:00 SGT 2010

Note that a Date object does not know anything about formatting by itself. If you print a Date object like you are doing in line 9 of your code, then a default format will be used to convert the Date to a String for display. If you want the date to be printed in a specific format, you must use the SimpleDateFormat object again to convert it to a string in the desired format:

 
Krish Khan
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jasper,

Appreciate your response.

Yeah when i format it again it will change to String which cannot be inserted into database because the data field is datetime type.

All I want is converting the String to datetime type so that i can insert into database (MS SQL).
 
Rob Spoor
Sheriff
Pie
Posts: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a PreparedStatement and its setTimestamp method:
That way there will be no problems with the formatting.
 
Krish Khan
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you give an example in my case.?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java™ Tutorials.
 
Krish Khan
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mates I got it...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic