如果用的ssh框架的话,在hibernate的表的映射文件中设置字段的type为java.sql.Date。
如果不是框架的话,例子:
String birthdayString ="2006-04-05";
java.util.Date birthday = new java.util.Date();
try ...{
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
birthday = sdf.parse(birthdayString);
System.out.println(birthday);
System.out.println(birthdayString);
} catch (ParseException e) ...{
System.out.println("String to Date error");
}
String birthdayString ="2006-04-05";
java.util.Date birthday = new java.util.Date();
try ...{
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
birthday = sdf.parse(birthdayString);
System.out.println(birthday);
System.out.println(birthdayString);
} catch (ParseException e) ...{
System.out.println("String to Date error");
}
用到的包:java.text.*
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
java.util.Date bdate = formatter.parse("2009-11-10");
endtime=time.getTime()-bdate.getTime();
System.out.println((endtime)/(3600*1000));
Date.format(“yyyy-MM-dd HH:mm:ss ”);