Java – How to get the number of years between two java.util.Date

gwtjava

How to get the number of years between two java.util.Date?

Note: using only java.util.Date

Best Solution

How accurate do you need to be? If approximate is good enough, then I'd do

long msDiff = date1.getTime() - date2.getTime();
int yearDiff = (int)(msDiff / 1000 / 60 / 60 / 24 / 365.25);