【发布时间】:2013-10-03 11:01:35
【问题描述】:
我的数据库表中有一些日期,例如“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; }
【问题讨论】:
-
所以你的问题是......?
-
我的问题是如何将 getdate() 传递给第二种方法以及如何从中减去两天。
-
首先我会让 getdate() 返回一个日期,然后您可以在添加 -2 天之前将此值用于 cal.setDate(getdate())。
-
@SubOptimal 方法 setDate(Date) 未定义为 Calendar 类型
-
你是对的......看看 API 显示:它应该是 cal.setTime(Date date) ;-)