1

我的数据库表中有一些日期,例如“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; }
4

4 回答 4

3

如果我理解正确,您可以在您的选择语句中一步完成

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

这是SQLFiddle演示

于 2013-10-03T11:14:15.230 回答
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;
}

PS:-getDate()返回一个String. 将其更改为返回一个Date.

于 2013-10-03T11:12:13.217 回答
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;

}
于 2013-10-03T11:15:52.630 回答
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
于 2013-10-03T11:29:06.683 回答