hesham
hesham

Reputation: 143

minus two days from returnd time from database

I have some dates in my database table like that " 2013-09-26 " .. all I trying to do is convert data which returned from database to Calender cuz i want to subtract TWO days from this date " 2013-09-26 " to be " 2013-09-24 " automatically .

This method return String of " 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;

}

This method should return String after getdate()-2 " I Mean minus two days from returned date "

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

Upvotes: 1

Views: 404

Answers (4)

Piyush
Piyush

Reputation: 2050

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;

}

calling this as

getDate("2013-09-26");

gives output

2013-09-24

Upvotes: 0

asantaballa
asantaballa

Reputation: 4048

I think you're on the right track, just include your calendar logic in your code.

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;

}

Upvotes: 0

peterm
peterm

Reputation: 92795

If I understand correctly you can do it in one step right in your select statement

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

Here is SQLFiddle demo

Upvotes: 3

Rahul
Rahul

Reputation: 45070

With a few changes to your testDate() method, you can do it.

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:- Your getDate() returns a String. Change that to return a Date.

Upvotes: 1

Related Questions