aspose file tools*
The moose likes JDBC and the fly likes Not able to convert string to date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Not able to convert string to date" Watch "Not able to convert string to date" New topic
Author

Not able to convert string to date

Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
Hi all,

I want to convert a string to Date in the format of "19/Apr/2010 17:46:57". Below is my code but this is not changing its giving in the format of "Mon Apr 19 17:54:39 GMT+05:30 2010"

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

A Date object doesn't have a changeable format. If you want to print the date object in a particular format, you will have to format it and print the resultant string -- formatting the date to a string, and parsing it back to a date, doesn't really do anything.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
A Date doesn't have a format, it's a number representing milliseconds.

Do you want to display it in the format "dd/MMM/yyyy HH:mm:ss"?

What do you get if you output System.out.println(s) after line 6?


SCJP, SCWCD
Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
Doug,
after 6th line am getting the same format i needed but its in string only "19/Apr/2010 17:46:57".
While am inserting that string into database Oracle its giving an exception as "date format picture ends before converting entire input string".


Regards,
Kartik
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
Are you specifying a format when you insert into Oracle? Does that format on the Oracle insert match the String? It sounds like it might be missing a bit..
Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
From oracle also i inserted in this way

insert into student values('1','19/Apr/2010 17:24:59').

its giving the same error as "date format picture ends before converting entire input string"
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
I think that what's happening there is that the format of your date string is not the format that Oracle is expecting.

if you try something like this:

insert into student values('1',TO_DATE('19/Apr/2010 17:24:59','DD/MON/YYYY HH24:MI:SS''))

then you are telling Oracle exactly what format date string is in and it should work.

Does that help?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Switch to PreparedStatement:
This problem is evidently an Oracle problem so I'm moving this to our JDBC forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
thanks doug it worked for me....
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
My pleasure
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to convert string to date