3

I am calculating the difference between two sql dates, TripStartDate and TripEndDate.
If TripStartDate= 2011-03-04 09:35:00 and TripEndDate = 2011-03-04 10:35:00 then I should get the number of day is 1 (because trip happened on that day).

Like this:

If TripStartDate = 2011-03-04 09:35:00 and TripEndDate = 2011-03-05 09:35:00 then method should return 2 days (because trip happened on both days).

If TripStartDate = 2011-03-04 09:35:00 and TripEndDate = 2011-04-04 09:35:00 then method should return 32 days. (because 28 days in march and 4 days in April).

Calculation should be based on only dates and month of year (not taking time in consideration). Please help me . Thanks in advance...

tim_yates
  • 167,322
  • 27
  • 342
  • 338
maaz
  • 3,534
  • 19
  • 61
  • 100
  • 3
    http://stackoverflow.com/questions/4792307/days-between-dates-java http://stackoverflow.com/questions/3838527/android-java-date-difference-in-days http://stackoverflow.com/questions/3384254/calculate-amount-of-days-between-events-android http://stackoverflow.com/questions/4984683/finding-the-difference-in-days-from-two-given-dates http://stackoverflow.com/questions/3329469/difference-in-days-between-two-java-dates http://stackoverflow.com/questions/2755835/duration-between-two-dates-in-groovy – tim_yates Mar 04 '11 at 07:42
  • Also, didn't you ask the exact same question here: http://stackoverflow.com/questions/5116936/how-to-find-the-number-of-days-between-two-dates-in-java-or-groovy – tim_yates Mar 04 '11 at 08:22

3 Answers3

2

In Java I guess you would drop the time and calculate the day difference

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.set(2011, 03, 04);
cal2.set(2011, 04, 04);
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();
long diff = milis2 - milis1;
long days = diff / (24 * 60 * 60 * 1000);

EDIT Quite surprisingly for me, that ^ code really doesn't work always... apparently there are some 'leap seconds' that mess up the maths. There are quite enough links already proposed to you in comments. I would go with joda time library.

m0s
  • 4,250
  • 9
  • 41
  • 64
  • Thanks for your response, can u please explain (24 * 60 * 60 * 1000) ? – maaz Mar 04 '11 at 07:11
  • 2
    @Hussain The time returned from getTimeInMillis is the standard time stamp, ie time in milliseconds since january 1st 1970, the difference of milis2 and millis1 will give you the difference between two dates in millisecond, but you need the the time in days, so dividing the milliseconds on 1000 to get in seconds, on 60 to get the minutes on 60 to get the hours on 24 to get the days. I hope it makes some sense and my math is correct. – m0s Mar 04 '11 at 07:40
  • 2
    @m0s -- you are way nicer than I am. +1 – Tim Perry Mar 04 '11 at 07:47
  • 1
    @Tim Perry ;D maybe way more bored. – m0s Mar 04 '11 at 07:50
  • Thanks for your patience... it returns 30 days... but i want it to return 32 days(28 days in March from 4 to 31 and 4 days in April from 1st to 4th) – maaz Mar 04 '11 at 08:24
  • This is incorrect in the case where daylight savings begins during the interval being calculated. Nothing to do with leap seconds - quite simply, if there's a 23-hour day in the mix, then dividing by `24 * 60 * 60 * 1000` is the wrong thing to do. I strongly recommend the new date features of Java 8, or Joda Time. The Java 8 class that you want is called `LocalDate`. – Dawood ibn Kareem Aug 15 '14 at 19:48
2

FYI, in Groovy this would be something along the lines of:

fourthMarch = Date.parse( 'yyyy-MM-dd', '2011-03-04' )
fifthMarch  = Date.parse( 'yyyy-MM-dd', '2011-03-05' )
fourthApril = Date.parse( 'yyyy-MM-dd', '2011-04-04' )

assert 2  == fifthMarch  - fourthMarch + 1
assert 32 == fourthApril - fourthMarch + 1

We need to add 1 as the dates are inclusive

tim_yates
  • 167,322
  • 27
  • 342
  • 338
0

Here's the usual way to do this, in Java 8.

LocalDate start = LocalDate.of(2011, 3, 4);  // Or whatever - this is Y, M, D
LocalDate end = LocalDate.of(2011, 4, 4);
return ChronoUnit.DAYS.between(start, end) + 1; 
                                         // The +1 is for the inclusive reckoning
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110