This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes java.sql.Date and DateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.sql.Date and DateFormat" Watch "java.sql.Date and DateFormat" New topic
Author

java.sql.Date and DateFormat

Dave Soto
Ranch Hand

Joined: Sep 15, 2001
Posts: 55
I have read various recommendations for DateFormat and SimpleDateFormat when dealing with JDBC and java.sql.Date. However, none of the methods in DateFormat or SimpleDateFormat seem to accept or return a java.sql.Date. Instead, they use a java.util.Date. So:
1) How does one work with a java.sql.Date in DateFormat and SimpleDateFormat?
2) Is there a simple way to convert between java.util.Date and java.sql.Date?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

This is what I do now. If there's a better way, I'd love to see it...
// eg. convert current date from java.util.Date ...
java.util.Date utilDate = new java.util.Date();
// ... to java.sql.Date
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

BTW, since java.sql.Date extends java.util.Date, you can pass it to SimpleDateFormat just as you would a java.util.Date. I believe DateFormat is an abstract class so you really want to use SimpleDateFormat or another concrete DateFormat descendant class.


[This message has been edited by JUNILU LACAR (edited October 06, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Dave Soto
Ranch Hand

Joined: Sep 15, 2001
Posts: 55
Thank you so much! Since both java.util.Date and java.sql.Date have a constructor that accepts milliseconds, and they both have the getTime() method that returns milliseconds, then that is the key to freely creating one from the other. It was staring me in the face the whole time. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.sql.Date and DateFormat