Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to cast the java.util.date to java.sql.date

 
lei zhang
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to cast the java.util.date to java.sql.date

please give me a help!



and i want to get the hours,seconds.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.sql.Date extends java.util.Date. That means you can cast java.sql.Date to java.util.Date, but not the other way round.

Both classes have methods called getHours, getMinutes and getSeconds.
 
Abhijith Prabhakar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps

/**
* Returns the sql.Date for the given util.Date
*
* @param oDate util.Date object
* @return sql.Date
*/
public static java.sql.Date convertToSqlDate(java.util.Date oDate) {
return (new java.sql.Date(oDate.getTime()));
}

/**
* Returns the util.Date for the given sql.Date
*
* @param sqlDate sql.Date object
* @return util.Date
*/
public static java.util.Date convertToDate(java.sql.Date sqlDate) {
return (new java.util.Date(sqlDate.getTime()));
}
 
lei zhang
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reply Abhijith Prabhakar :

thank you give me the methods to deal with this,but ,i do it like you say that,but i translate it in this way what you say,i didn't get the hours,minutes,and seconds only the day,month,and year ,can you tell me why?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "i didn't get the hours,minutes,and seconds"? The getHours, getMinutes and getSeconds methods must return something. If you mean that the return value is always zero, have you checked that the DB has the exact values for those fields?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic