Win a copy of Learning Regular Expressions this week in the General Computing 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Not able to convert string to date  RSS feed

 
Ranch Hand
Posts: 104
  • 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"

 
author
Sheriff
Posts: 23590
138
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
 
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: 104
  • 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: 104
  • 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?
 
Sheriff
Posts: 21461
96
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: 104
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!