Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 415,728 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,736 people online right now.Registration is fast and FREE... Join Now!



Date data type and format issue Rate Topic: -----

#1 kensington  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 08-October 07


Dream Kudos: 0

Share |

Date data type and format issue

Posted 19 November 2007 - 04:59 PM

I have a Date insert that is formatted into an Oracle 9i database and it works.
But I had to make the Date variable a varchar2 in Oracle to get it to work:
java.sql.Timestamp myd = new java.sql.Timestamp(new java.util.Date().getTime());
String sub_date = new SimpleDateFormat("mm/dd/yy , h:mm a").format(myd);
String query = "insert into dept(location, sub_date) values(?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);				  
pstmt.setString(1, "Jersey");
pstmt.setString(2, sub_date); 
pstmt.executeUpdate();


I also tried pstmt.setTimestamp(2, sub_date) with a Date data type in Oracle and I could get the date to insert but couldnt format it.

Please advise how I can get the Oracle Date datatype to work where I can also format it?

This post has been edited by kensington: 19 November 2007 - 06:16 PM

Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1435
  • View blog
  • Posts: 8,316
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: Date data type and format issue

Posted 19 November 2007 - 06:42 PM

After you insert the date, is it correct even though it is not in the format you want? Because the idea is that you insert the data and you format AFTER you pull it out of the database. Formatting is a presentational layer while databases are a information structure layer. As long as the date is correct in the database (not reading something crazy like December 31 1969 00:00:00 when you wanted todays date) then you should worry about formatting after you pull it out.

Also try using a four digit year with no time part yet. Just try dd/mm/yyyy and see if it goes in fine. Then worry about the time formatting. Make sure that all your parameters are java.sql.Date objects too.

Lastly, when you format it the way you want and try to insert and it fails, what error are you getting for the reason it won't insert?

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • Icon

Reputation: 40
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05


Dream Kudos: 0

Re: Date data type and format issue

Posted 20 November 2007 - 01:13 AM

Instead of

Quote

pstmt.setString(2, sub_date);

use the setDate function of your PreparedStatement, and the driver will take care of the formatting, escaping and everything.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users