• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.SimpleDateFormat

 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a PreparedStatement and bind the date parameter, then you don;t have to worry about formatting issues.
 
Yelamuri Chandu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • 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?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic