Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in days between two dates in Java?

I need to find the number of days between two dates: one is from a report and one is the current date. My snippet:

  int age=calculateDifference(agingDate, today); 

Here calculateDifference is a private method, agingDate and today are Date objects, just for your clarification. I've followed two articles from a Java forum, Thread 1 / Thread 2.

It works fine in a standalone program although when I include this into my logic to read from the report I get an unusual difference in values.

Why is it happening and how can I fix it?

EDIT :

I'm getting a greater number of days compared to the actual amount of Days.

public static int calculateDifference(Date a, Date b) {     int tempDifference = 0;     int difference = 0;     Calendar earlier = Calendar.getInstance();     Calendar later = Calendar.getInstance();      if (a.compareTo(b) < 0)     {         earlier.setTime(a);         later.setTime(b);     }     else     {         earlier.setTime(b);         later.setTime(a);     }      while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR))     {         tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));         difference += tempDifference;          earlier.add(Calendar.DAY_OF_YEAR, tempDifference);     }      if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR))     {         tempDifference = later.get(Calendar.DAY_OF_YEAR) - earlier.get(Calendar.DAY_OF_YEAR);         difference += tempDifference;          earlier.add(Calendar.DAY_OF_YEAR, tempDifference);     }      return difference; } 

Note :

Unfortunately, none of the answers helped me solve the problem. I've accomplished this problem with the help of Joda-time library.

like image 371
Venkat Avatar asked Jul 21 '10 13:07

Venkat


People also ask

How do I get the number of days between two dates in Java?

The Period class has a between() method - just as the previously discussed ChronoUnit . This method takes in two LocalDate objects, one representing the starting date, and the second being the end date. It returns a Period consisting of the number of years, months, and days between two dates.

How do you find the difference between two dates in days?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.

How do I calculate the number of days between two dates in Java 8?

In Java 8, we can use ChronoUnit. DAYS. between(from, to) to calculate days between two dates.


2 Answers

I would suggest you use the excellent Joda Time library instead of the flawed java.util.Date and friends. You could simply write

import java.util.Date; import org.joda.time.DateTime; import org.joda.time.Days;  Date past = new Date(110, 5, 20); // June 20th, 2010 Date today = new Date(110, 6, 24); // July 24th  int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); // => 34 
like image 164
Adam Schmideg Avatar answered Oct 03 '22 09:10

Adam Schmideg


I might be too late to join the game but what the heck huh? :)

Do you think this is a threading issue? How are you using the output of this method for example? OR

Can we change your code to do something as simple as:

Calendar calendar1 = Calendar.getInstance();     Calendar calendar2 = Calendar.getInstance();     calendar1.set(<your earlier date>);     calendar2.set(<your current date>);     long milliseconds1 = calendar1.getTimeInMillis();     long milliseconds2 = calendar2.getTimeInMillis();     long diff = milliseconds2 - milliseconds1;     long diffSeconds = diff / 1000;     long diffMinutes = diff / (60 * 1000);     long diffHours = diff / (60 * 60 * 1000);     long diffDays = diff / (24 * 60 * 60 * 1000);     System.out.println("\nThe Date Different Example");     System.out.println("Time in milliseconds: " + diff  + " milliseconds.");     System.out.println("Time in seconds: " + diffSeconds  + " seconds.");     System.out.println("Time in minutes: " + diffMinutes  + " minutes.");     System.out.println("Time in hours: " + diffHours  + " hours.");     System.out.println("Time in days: " + diffDays  + " days.");   } 
like image 21
Suji Avatar answered Oct 03 '22 09:10

Suji