• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Not able to convert string to date

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks doug it worked for me....
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic