Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to convert string to date

 
Kartik Tal
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kartik Tal
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Tal
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Switch to PreparedStatement:
This problem is evidently an Oracle problem so I'm moving this to our JDBC forum.
 
Kartik Tal
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks doug it worked for me....
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic