Java 从数据库返回的时间减去两天
我的数据库表中有一些日期,如“2013-09-26”。。我所要做的就是把从数据库返回的数据转换成日历,因为我想从这个日期“2013-09-26”减去两天,自动变成“2013-09-24” 此方法返回字符串“2013-09-26” 此方法应在getdate()之后返回字符串-2“我的意思是从返回的日期减去两天”Java 从数据库返回的时间减去两天,java,mysql,time,Java,Mysql,Time,我的数据库表中有一些日期,如“2013-09-26”。。我所要做的就是把从数据库返回的数据转换成日历,因为我想从这个日期“2013-09-26”减去两天,自动变成“2013-09-24” 此方法返回字符串“2013-09-26” 此方法应在getdate()之后返回字符串-2“我的意思是从返回的日期减去两天” 只需对testDate()方法进行一些更改,就可以完成此任务 public static String testDate() throws ClassNotFoundException,
只需对
testDate()
方法进行一些更改,就可以完成此任务
public static String testDate() throws ClassNotFoundException,
ReflectiveOperationException, Exception {
if (getDayId() == 1) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
cal.setTime(getdate()); // Calling getDate() method and setting the date before subtracting 2 days.
cal.add(Calendar.DATE, -2);
return dateFormat.format(cal.getTime());
}
return null;
}
p.S:-您的
getDate()
返回一个字符串。更改它以返回一个日期
只需对testDate()
方法进行一些更改,就可以执行此操作
public static String testDate() throws ClassNotFoundException,
ReflectiveOperationException, Exception {
if (getDayId() == 1) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
cal.setTime(getdate()); // Calling getDate() method and setting the date before subtracting 2 days.
cal.add(Calendar.DATE, -2);
return dateFormat.format(cal.getTime());
}
return null;
}
p.S:-您的getDate()
返回一个字符串。如果我理解正确,您可以在select语句中一步完成,将其更改为返回日期
选择APSU日期-间隔2天
从阿森特
其中,day_id=1
下面是演示如果我理解正确,您可以在select语句中一步完成
选择APSU日期-间隔2天
从阿森特
其中,day_id=1
这里是演示我认为你的思路是正确的,只需在代码中包含日历逻辑即可
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);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
c.add(Calendar.DATE, -2);
date.setTime( cal.getTime() );
return date.toString() ;
}
rs.close();
stmt.close();
con.close();
}
catch (SQLException e){
}
return null;
}
我认为您的思路是正确的,只需在代码中包含日历逻辑即可
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);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
c.add(Calendar.DATE, -2);
date.setTime( cal.getTime() );
return date.toString() ;
}
rs.close();
stmt.close();
con.close();
}
catch (SQLException e){
}
return null;
}
}
称之为
getDate("2013-09-26");
输出
2013-09-24
}
称之为
getDate("2013-09-26");
输出
2013-09-24
所以你的问题是…?我的问题是如何将getdate()传递给第二个方法,以及如何从中减去两天。首先,我会让getdate()返回一个日期,然后你可以在添加-2天之前将此值用于cal.setDate(getdate())。@次优方法setDate(Date)对于Calendar类型没有定义。你是对的。。。看看API就会发现:它应该是cal.setTime(Date-Date);-)所以你的问题是…?我的问题是如何将getdate()传递给第二个方法,以及如何从中减去两天。首先,我会让getdate()返回一个日期,然后你可以在添加-2天之前将此值用于cal.setDate(getdate())。@次优方法setDate(Date)对于Calendar类型没有定义。你是对的。。。看看API就会发现:它应该是cal.setTime(Date-Date);-)如果只需要更新日期,那么从java程序运行查询是没有意义的。最好的做法是按照这个答案运行update语句。如果您只需要更新日期,那么从java程序运行查询是没有意义的。最好的做法是按照这个答案运行update语句。