wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes string to date conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string to date conversion" Watch "string to date conversion" New topic
Author

string to date conversion

Krish Khan
Ranch Hand

Joined: Dec 14, 2009
Posts: 46
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

Joined: Jan 10, 2002
Posts: 60774
    
  65

As this has nothing to do with Servlets, it's been moved to a more appropriate location.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

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


-Chiru
Krish Khan
Ranch Hand

Joined: Dec 14, 2009
Posts: 46
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

Joined: Oct 25, 2008
Posts: 2700

Date only has a format for debugging purposes. You can use the DateFormat to get it in format that you want.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Krish Khan
Ranch Hand

Joined: Dec 14, 2009
Posts: 46
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

Use a PreparedStatement and its setTimestamp method:
That way there will be no problems with the formatting.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Krish Khan
Ranch Hand

Joined: Dec 14, 2009
Posts: 46
Hi,

Can you give an example in my case.?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Go through the Java™ Tutorials.
Krish Khan
Ranch Hand

Joined: Dec 14, 2009
Posts: 46
Thanks Mates I got it...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: string to date conversion
 
Similar Threads
conversion of date
Convert Java.sql.Date to String or Java.util.Date
Read Excel values and store in to Database
how to change the date,Time format after accessing it from the database
Converting from string to date