Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting From String to java.sql.Date  RSS feed

 
Gabriel Cane
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to know, using non-deprecated methods, how I could take a String and convert it to a java.sql.Date object.
For example, my web application will be receiving three pieces of information:
month, day, year.
I've been fooling around with the Calendar object and I can't quite figure it out.
Thanks.
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some code to get you started, does very basic date validation:

Also have a look at java.text.SimpleDateFormat for further info.
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention, to go from a java.util.Date to java.sql.Date, use something like:
java.util.Date date = (see above);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!