Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

string to date conversion

 
Ranch Hand
Posts: 46
  • 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!
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • 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
  • 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


 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
  • 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).
 
Sheriff
Posts: 22000
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you give an example in my case.?
 
Marshal
Posts: 70304
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java™ Tutorials.
 
Krish Khan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mates I got it...
 
eat bricks! HA! And here's another one! And a tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic