• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

java.util.SimpleDateFormat

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following is a code snippet I have used,

Date useddate=new Date();
SimpleDateFormat useddateformat=new SimpleDateFormat("yyyyMMddkkmmss");
String enterdate=new String(useddateformat.format(useddate).toString());

I am using this enterdate variable to insert a record to the database. But I get a SQLException due to this part. I am getting a error every day from 12 00 AM to 12 59 AM. Simply my sql fails. Thats because of the "00" in 24 hour clock. So how do I overcome this problem?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a PreparedStatement and bind the date parameter, then you don;t have to worry about formatting issues.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use convertion properly at both the ends..

Use following formats

SimpleDateFormat useddateformat=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");


and at Database side use as follows

v_date := TO_DATE (in_date_char,'mm/dd/yyyy hh24:mi:ss');

this will work.
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Yelamuri and Paul. But I want to clarify my code. I also replaced the 'k' with 'H' but still I get that error.

I think my coding is perfectly correct so it should work. Any suggestions?
 
Sheriff
Posts: 22647
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Paul said, use a PreparedStatement, and use the setTimestamp method. To create a new Timestamp, do the following:
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rob. It works fine what you have told me. But I would like to know what I have done wrong in my previous coding.

Thank you
[ January 14, 2008: Message edited by: Dilshan Edirisuriya ]
 
Rob Spoor
Sheriff
Posts: 22647
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of passing the Date object as a string to the database server, and hoping it will see it correctly, you are passing the Date object as what it is - a date/time. The database server will know what to do with it, without you having to try and get it right, hoping the date format is correct.

Please note that there are three date/time classes in java.sql:
- Date usually only stores the date part, and ignores the time
- Time usually does the opposite
- Timestamp stores both
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the database table I am storing that value as a string. Thats why I am passing that as a string.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dilshan Edirisuriya:
In the database table I am storing that value as a string. Thats why I am passing that as a string.



That sounds unlikely. If you were using a text data type there would be no SQLException; you can store what you like in there, the format doesn't matter.

If this is a date, why use a text type at all?
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes date/time value should have been stored as a date/time. But I did that because later I can simply retrive and format it as I want because I have several distinct formatting patterns in my programme.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic