【问题标题】:minus two days from returnd time from database从数据库返回的时间减去两天
【发布时间】: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) ;-)

标签: java mysql time


【解决方案1】:

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

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

这里是SQLFiddle演示

【讨论】:

  • 如果您只需要更新日期,那么从您的 java 程序运行查询是没有意义的。最好的办法是遵循这个答案并运行更新语句。
【解决方案2】:

只需对您的 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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:
      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        • 2012-09-04
        • 1970-01-01
        • 2018-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多