• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.sql.Date and DateFormat

 
Dave Soto
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7598
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Dave Soto
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic