Posted By

habib123 on 04/29/10


Tagged

date difference


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

darkapple


Date difference


 / Published in: Java
 

  1. Calendar calendar1 = Calendar.getInstance();
  2. Calendar calendar2 = Calendar.getInstance();
  3. DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  4. Date date = new Date();
  5. try {
  6. date = formatter.parse(trailLoginDate);
  7. } catch (Exception e) {
  8. // TODO: handle exception
  9. }
  10. calendar1.setTime(date);
  11. calendar2.setTime(new Date());
  12. long milliseconds1 = calendar1.getTimeInMillis();
  13. long milliseconds2 = calendar2.getTimeInMillis();
  14. long diff = milliseconds2 - milliseconds1;
  15. long diffDays = diff / (24 * 60 * 60 * 1000);
  16. days = (int)diffDays;

Report this snippet  

You need to login to post a comment.