-1

how can i get days from two dates one date will be from the data base and i can retrive it properly but how can i compare it with the current system date? im able to store current date into database using settimestamp function but cant use it for comparing i have used jodatime but cant get it through

     // TODO add your handling code here:
       JOptionPane.showMessageDialog(null,"Fine test");
        try{
            conn = Connect.ConnectDB();  
            String qw="select bookid,date from borrow";
            //String date1="Select Cast ((JulianDay(ToDate) - JulianDay(FromDate)) As Integer)";
        pst = conn.prepareStatement(qw);
 rs=pst.executeQuery();
 while(rs.next())
 {
     String fine=rs.getString("date");
     //String today=Timestamp(System.currentTimeMillis()
     //String date1="Select Cast ((JulianDay() - JulianDay(fine)) As Integer)";
    //pst = conn.prepareStatement(date1);
    //DateTime ret1=formatter.parseDateTime(df.format(dateobj));
   // DateTime dt = formatter.parseDateTime(fine);
//days = Days.daysBetween(dt,dt).getDays();
    // int n=ChronoUnit.DAYS.between(fine,fine);
      JOptionPane.showMessageDialog(null,"Days are"+fine);
 }
        }
        catch(Exception e)
        {
             JOptionPane.showMessageDialog(null,"Fine error"+e);
        }
    }                                              

    private void formWindowClosed(java.awt.event.WindowEvent evt) {                                  
        // TODO add your handling code here:
        try{
        conn.commit();
        conn.close();
        }
        catch(Exception e)
        {}
    }                                 
GB Golden
  • 43
  • 1
  • 12

1 Answers1

-1
public class Main {
    public static int getDifferenceDays(Date d1, Date d2) {
        int daysdiff=0;
        long diff = d2.getTime() - d1.getTime();
        long diffDays = diff / (24 * 60 * 60 * 1000)+1;
        daysdiff = (int) diffDays;
        return daysdiff;
    }

    public static void main(String[] args) throws ParseException {
        String dateStart = "11/21/2016";
        String dateStop =  "11/22/2016";

        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

        Date d1 = format.parse(dateStart);
        Date d2 = format.parse(dateStop);

        System.out.println(getDifferenceDays(d1,d2));
    }
}

how about calculate from milliseconds.one day: 24 * 60 * 60 * 1000 = 86400000 milliseconds.

example output:
11/21/2016
11/22/2016
Days between: 2
  • let me give it a try @stokhosT – GB Golden Nov 21 '16 at 02:49
  • i have to use the current system date – GB Golden Nov 21 '16 at 02:50
  • 3
    This is incorrect. It fails in any locale that uses daylight savings, if the first date is in the winter and the second date is in the summer. Not all days have 24 hours. Better to use either `java.time` or JodaTime, than to try and implement the calculation yourself. – Dawood ibn Kareem Nov 21 '16 at 03:52
  • Yes correct. but if you are looking day diff with truncated date then result will be correct. – newuserua_ext Nov 21 '16 at 06:06
  • 1
    The OP says they are using jodatime. This API has a clearly documented function `Days.between()` for what they want. No need to reinvent the wheel – David Rawson Nov 21 '16 at 06:43
  • i cant find the solution i want to get days between the current date and the date in fine varible – GB Golden Nov 21 '16 at 13:08
  • `Days.daysBetween(DateTime.now(), fine).getDays()` but you've got other people to write your code for you where 2 google searches ("how do i get the current date using joda time?", "how do i get the days between two dates using joda time") and a bit of reasoning would have solved it for yourself. – David Rawson Nov 21 '16 at 18:17