How to find the number of days between two dates in java or groovy?
Solution 1
For the groovy solution you asked for you should consider using this:
use(groovy.time.TimeCategory) {
def duration = date1 - date2
println "days: ${duration.days}, Hours: ${duration.hours}"
}
It's very easy to understand and extremely readable. You asked for a example how this can be used in an easy method which calculates the days between two dates. So here is your example.
class Example {
public static void main(String[] args) {
def lastWeek = new Date() - 7;
def today = new Date()
println daysBetween(lastWeek, today)
}
static def daysBetween(def startDate, def endDate) {
use(groovy.time.TimeCategory) {
def duration = endDate - startDate
return duration.days
}
}
}
If you run this example it will print you 7
. You can also enhance this method by using before()
and after()
to enable inverted dates.
Solution 2
It's a well worn line, but for Dates use JodaTime.
Here's how to calculate date intervals using JodaTime.
Days days = Days.daysBetween(new DateTime(millis1), new DateTime(millis2));
int daysBetweenDates = days.getDays();
Solution 3
GregorianCalendar cal1 = new GregorianCalendar(2011,2,9);
GregorianCalendar cal2 = new GregorianCalendar(2011,2,19);
long ms1 = cal1.getTime().getTime();
long ms2 = cal2.getTime().getTime();
long difMs = ms2-ms1;
long msPerDay = 1000*60*60*24;
double days = difMs / msPerDay;
Solution 4
In groovy all you need is:
date2 - date1
which returns an integer representing the number of days between the two dates.
Or if you need to guard against reversal of order between the two Date instances (the operation returns negative numbers when the first operand is earlier than the second):
Math.abs(date2 - date1)
The above examples use the groovy date.minus(date) operator implementation which returns the number of days between the two dates.
Example groovy shell session:
$ groovysh
Groovy Shell (2.4.8, JVM: 1.8.0_111)
Type ':help' or ':h' for help.
groovy:000> x = new Date(1486382537168)
===> Mon Feb 06 13:02:17 CET 2017
groovy:000> y = new Date(1486000000000)
===> Thu Feb 02 02:46:40 CET 2017
groovy:000> x - y
===> 4
or if you need a method:
int daysBetween(date1, date2) {
Math.abs(date2 - date1)
}
Related videos on Youtube
maaz
Updated on July 01, 2022Comments
-
maaz almost 2 years
I have a method which uses following logic to calculate difference between days.
long diff = milliseconds2 - milliseconds1; long diffDays = diff / (24 * 60 * 60 * 1000);
but I want for ex,
9th feb 2011 to 19th feb 2011
should return me11
days irrespective of second or milliseconds consideration. How can I achieve this? -
maaz about 13 yearsHi thanks for your answer.. i didn't understand this.. how can make this as a function where i will pass two date object and it should return me difference between days?
-
Christopher Klewes about 13 yearsI added you an example for the function, please consider the improvement of your
daysBetween()
method with thebefore()
check. -
maaz about 13 yearsbetween 9th feb 2011 and 9th feb 2011 the difference should be 1 9th feb 2011 09:00 to 19th feb 14:00 should give me 11 days .
-
Vishy about 13 yearsThis method will give 11 days regardless of the time. It will even give 11 for
9th feb 2011 23:00
and19th feb 2011 01:00
-
rudolfson almost 9 yearsSince Java 8 you could use the JDK's time API, too. See docs.oracle.com/javase/8/docs/api/index.html?java/time/…. In your case you could possibly use
Period.between(date1, date2)
-
Matias Bjarland about 7 yearsas per below answer, for this specific problem and a groovy solution, you don't actually need to use TimeCategory,
date1 - date2
is enough. -
Mario over 4 years
date2 - date1
is so groovy -
Pablo Pazos over 2 yearsthis is true for newer versions of Groovy, maybe this wasn't available back then
-
Matias Bjarland over 2 yearsI assume you are talking about the
date.minus(date)
method? Seems it's been in groovy since 1.6.0 so I guess that depends on what we mean by newer versions of groovy. I believe groovy 1.6.0 was released 2009.