从数据库返回时间减去两天

时间:2013-10-03 11:01:35

标签: java mysql time

我在我的数据库表中有一些日期,比如“2013-09-26”..我想要做的就是将从数据库返回的数据转换为Calender cuz我想从这个日期减去两天“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()之后返回String - 2“我的意思是从返回日期开始减去两天”

 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; }

4 个答案:

答案 0 :(得分:3)

如果我理解正确,你可以在选择陈述中一步完成

SELECT apssent_date - INTERVAL 2 DAY 
  FROM apsent 
 WHERE day_id = 1

这是 SQLFiddle 演示

答案 1 :(得分:1)

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()会返回String。将其更改为返回Date

答案 2 :(得分:0)

我认为您走在正确的轨道上,只需在代码中包含日历逻辑。

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;

}

答案 3 :(得分:0)

public static String  getDate(String date)
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try 
{
    Date inputDate = dateFormat.parse(date);

    Calendar cal = Calendar.getInstance();
    cal.setTime(inputDate);

    cal.add(Calendar.DATE, -2);

    return dateFormat.format(cal.getTime());
} 
catch (ParseException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return null;

}

将其称为

getDate("2013-09-26");

给出输出

2013-09-24