Date difference


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.