This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes dates and JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "dates and JDBC" Watch "dates and JDBC" New topic

dates and JDBC

Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Hey Java Experts,

I am trying to take a date and put it into MySQL. I know there are a lot of ways to do this, but I am trying to use Simple Date Format "MM-DD-YYYY". I know how to parse a string and produce a date, but how would I take a date and parse it to a string in the MM-DD-YYYY format?

I've read about toPattern() and applyPattern() . Would either of these be helpful?

Thanks alot. I know your time is valuable.

Just Another Guy Hooked on Java
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

If you want the same pattern all the time, you can just add it to the construction of SimpleDateFormat

e.g. like this:

Otherwise you can do it like this sample:

Regards, Rene Larsen
Dropbox Invite
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Why would you want to convert a Date to a String? If you use a PreparedStatement then you can use the setDate method to work with the Date object directly. No need for any string operations - let JDBC do the work for you.
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48

Because I haven't gotten to "PreparedStatements" on the JavaTrail!

I'll be sure to read up on them!

I agree. Here's the link:
subject: dates and JDBC
It's not a secret anymore!