File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes java.util.SimpleDateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.util.SimpleDateFormat" Watch "java.util.SimpleDateFormat" New topic
Author

java.util.SimpleDateFormat

Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
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?


Dilshan Edirisuriya SCJP1.4, SCWCD1.4, SCBCD 5
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Use a PreparedStatement and bind the date parameter, then you don;t have to worry about formatting issues.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Yelamuri Chandu
Greenhorn

Joined: Jan 08, 2008
Posts: 15
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

Joined: Apr 22, 2006
Posts: 299
Thank you
Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
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

Joined: Oct 27, 2005
Posts: 19726
    
  20

As Paul said, use a PreparedStatement, and use the setTimestamp method. To create a new Timestamp, do the following:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
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

Joined: Oct 27, 2005
Posts: 19726
    
  20

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

Joined: Apr 22, 2006
Posts: 299
In the database table I am storing that value as a string. Thats why I am passing that as a string.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Apr 22, 2006
Posts: 299
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.SimpleDateFormat