Calculate difference of 2 dates in java
How to calculate the difference between current date and a date from sql date? { Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/lmsdb?autoReconnect=true&useSSL=false","root","root"); Statement stt=conn.createStatement(); String SQuerry="SELECT RetrunDate FROM borrowedbooks WHERE MemberID='"+MemberID+"' AND BookID='"+BookID+"'"; ResultSet rss=stt.executeQuery(SQuerry); Date RetrunDate; while(rss.next()) { RetrunDate = new Date(rss.getDate("RetrunDate").getTime()); Date today=new Date(); Calendar myNextCalender=Calendar.getInstance(); myNextCalender.set(2021,0,1); //Here i need to add "RetrunDate, but it's not working" Date nextYearDate=myNextCalender.getTime(); long days=daysBetween(today,nextYearDate); SimpleDateFormat sdf=new SimpleDateFormat("MMM dd, YYYY"); String todaysdate=sdf.format(today); String newDate=sdf.format(nextYearDate); JOptionPane.showMessageDialog(this,days,"No of days",JOptionPane.INFORMATION_MESSAGE); } } private long daysBetween(Date one,Date two) { long differnce=(one.getTime()-two.getTime())/864000000; return Math.abs(differnce); } Have an issue when setting the "RetrunDate" into the calender format..