Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP - java.sql.Date

 
Jason Barker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my first post so if I am doing this incorrectly, please don't flame me! Thanks.
If I have a month, day and year and I want to create a Date object, what is the best way to go about this? Is there a way to get a Date object based on the current date? If I have a GregorianCalendar object, is there a way to convert from it to java.sql.Date?

Thanks in advance!
Jason
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I have a month, day and year and I want to create a Date object, what is the best way to go about this?

Where year, month and day are integers:
new java.sql.Date(year, month, day);

Is there a way to get a Date object based on the current date?

java.sql.Date doesn't have a no-argument constructor to get the current time, but java.util.Date does:
new java.sql.Date(new java.util.Date().getTime());

If I have a GregorianCalendar object, is there a way to convert from it to java.sql.Date?

new java.sql.Date(gregorianCalendar.getTimeInMillis());
 
Jason Barker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! That does the trick!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic