// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

public class DateDiff {
    public static void main(String[] args) throws SQLException {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "root", "");
        Statement stmt = con.createStatement();
        stmt.executeUpdate("CREATE TABLE dd (id INTEGER NOT NULL, d DATETIME, PRIMARY KEY(id))");
        PreparedStatement pstmt1 = con.prepareStatement("INSERT INTO dd VALUES(?,?)");
        pstmt1.setInt(1, 1);
        pstmt1.setTimestamp(2,  new Timestamp(System.currentTimeMillis()));
        pstmt1.executeUpdate();
        pstmt1.setInt(1, 2);
        pstmt1.setTimestamp(2,  new Timestamp(System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000L));
        pstmt1.executeUpdate();
        pstmt1.setInt(1, 3);
        pstmt1.setTimestamp(2,  new Timestamp(System.currentTimeMillis() - 14 * 24 * 60 * 60 * 1000L));
        pstmt1.executeUpdate();
        pstmt1.close();
        PreparedStatement pstmt2 = con.prepareStatement("SELECT id FROM dd WHERE DATEDIFF(?,d) > ?");
        pstmt2.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
        pstmt2.setInt(2, 5);
        ResultSet rs2 = pstmt2.executeQuery();
        while(rs2.next()) {
            System.out.println(rs2.getInt(1));
        }
        rs2.close();
        pstmt2.close();
        stmt.executeUpdate("DROP TABLE dd");
        stmt.close();
        con.close();
    }
}
