我怎样才能在两个日期之间获得日期?

时间:2016-11-21 02:18:30

标签: java netbeans jodatime

我如何从两个日期获得一天的日期将来自数据库,我可以正确地检索它,但我如何将其与当前系统日期进行比较?我能够使用settimestamp函数将当前日期存储到数据库中但不能用于比较我已经使用了jodatime但是无法通过

     // 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)
        {}
    }                                 

1 个答案:

答案 0 :(得分:-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));
    }
}

如何从毫秒计算一天:24 * 60 * 60 * 1000 = 86400000毫秒。

example output:
11/21/2016
11/22/2016
Days between: 2