0
private static long daysBetween(Date one, Date two) { 
long difference = (one.getTime()-two.getTime())/86400000;
 return Math.abs(difference); }

Differrence between days without date class My Program has to calculate difference between two dates (issue date and return date) for my library book management system and calculate fine if differnce is more than 7

  • 2
    Use JodaTime or Java 8's new Time API – MadProgrammer Nov 12 '15 at 04:15
  • Maybe I'm not understanding the question, but "without the date class" meaning without passing in classes of type Date into a method? – subdigit Nov 12 '15 at 05:24
  • Yes ,My Program has to calculate difference between two dates (issue date and return date) for my library book management system and calculate fine if differnce is more than 7 – haribo komari Nov 13 '15 at 09:43
  • The safest way to do this, without using java 8 or joda time would to be use a Calendar and keep rolling the date. Using the milliseconds and converting to days directly can lead to inaccuracies due to daylight saving time. – PeterK Nov 13 '15 at 10:10

1 Answers1

0

You can try this

    long difference = date2.getTime() - date1.getTime();
    System.out.println ("Days: " + TimeUnit.DAYS.convert(difference, TimeUnit.MILLISECONDS));
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
  • But be careful because [not all days are exactly 86400000 milliseconds long](https://en.wikipedia.org/wiki/Daylight_saving_time#Procedure). – user11153 Nov 13 '15 at 10:23