我的数据库表中有一些日期,例如“2013-09-26”..我要做的就是将从数据库返回的数据转换为日历因为我想从这个日期“2013-09-26”中减去两天自动成为“2013-09-24”。
此方法返回“2013-09-26”的字符串
public String getdate() throws ClassNotFoundException, ReflectiveOperationException, Exception{
try {
Dbconnection NewConnect = new Dbconnection();
Connection con = NewConnect.MakeConnect();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select apssent_date from apsent where day_id = 1" ) ;
Date date ;
while(rs.next()){
date = rs.getDate(1);
return date.toString() ;
}
rs.close();
stmt.close();
con.close();
}
catch (SQLException e){
}
return null;
}
此方法应在 getdate()-2 “我的意思是从返回日期减去两天”之后返回 String
public String testDate() throws ClassNotFoundException,
ReflectiveOperationException, Exception {
if (getDayId() == 1) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -2);
// java.util.Date date = getdate() ;
return dateFormat.format(cal.getTime());
}
return null; }