• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

java.sql.Date and DateFormat

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 17699
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic