
I want to subtract a date that i retrieve from mysql database with the system date that i retrieve from java. I want the difference between the two date in the integer form. how do i do it ? Please help me. Thank you !

import java.sql.*;
import java.util.*;
public class DateDifference{
public int daysBetween(java.util.Date d1, java.util.Date d2){
return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
}
public DateDifference(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select dob from student where id=7");
java.util.Date date=null;
if(rs.next()){
java.sql.Date d = rs.getDate("dob");
date = new java.util.Date(d.getTime());
}
java.util.Date currentDate=new java.util.Date();
System.out.println("Days= "+daysBetween(date,currentDate));
}
catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args){
DateDifference inner = new DateDifference();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.