1

I have integers representing the month, day and year of two dates. How do I calculate if the difference between them is greater than 20 years? I used this on registration of new users.

ethan
  • 780
  • 7
  • 20
Damir
  • 54,277
  • 94
  • 246
  • 365

6 Answers6

5

Use Joda-Time, period! :)

Although "greater than 20 years" might be somewhat dependent on which kind of Calendar you're talking about, when you start counting, leap years, or daylight savings, Joda-Time will give you more flexibility that the util.Calendar class. Working with util.Date is not recommended and counting millisecond (or other things like that) will probably lead to buggy code.

JODA-TIME Code Samples:

Given:

int year1 = 2012, month1 = 2, day1 = 7;
int year2 = 1987, month2 = 7, day2 = 23;
//You can include a TimeZone if needed in the constructors below
DateTime dateTime1 = new DateTime(year1, month1, day1, 0, 0); //2012-02-07T00:00:00.000-05:00
DateTime dateTime2 = new DateTime(year2, month2, day2, 0, 0); //1987-07-23T00:00:00.000-04:00

Option 1, boring...

DateTime twentyYearsBefore = dateTime1.minusYears(20); //1992-02-07T00:00:00.000-05:00
if(dateTime2.compareTo(twentyYearsBefore) == -1)
    System.out.println("The difference between the dates is greater than 20 years");

Option 2, good stuff!

Days d = Days.daysBetween(dateTime1, dateTime2);
int days = d.getDays(); //-8965 days
System.out.println("There are " + days + " days between the two dates");

Option 3, rocket science!!! ;)

Period periodDifference = new Period(dateTime1, dateTime2);
System.out.println(periodDifference); //prints: P-24Y-6M-2W-1D

Of course the Period class has a ton of methods to get only the relevant fields. Click the following for the APIs of DateTime and Days

Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
  • Thanks for the link to Joda, I hadn't run across that before, seems like a very useful date / time implementation. – FoamyGuy Feb 09 '12 at 14:39
2
Calendar Day1 = Calendar.getInstance();
Day1.set(Calendar.DAY_OF_MONTH, day1 - 1);
Day1.set(Calendar.MONTH, month1 - 1); // 0-11 so 1 less
Day1.set(Calendar.YEAR, year1);

Calendar Day2 = Calendar.getInstance();
Day2.set(Calendar.DAY_OF_MONTH, day2 - 1);
Day2.set(Calendar.MONTH, month2 - 1); // 0-11 so 1 less
Day2.set(Calendar.YEAR, year2);

long twenty_years = 31536000000 * 20;
long diff;
if(Day1.compareTo(Day2) == 1)
    long diff = Day1.getTimeInMillis() - Day2.getTimeInMillis();
else
    long diff = Day2.getTimeInMillis() - Day1.getTimeInMillis();

if(diff > twenty_years){
    // do something
}
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
2

I think something simple as should do the trick.

double date1 = year + (month / 12.0) + (day / 365.0);
double date2 = year2 + (month2 / 12.0) + (day2 / 365.0);

if(Math.abs(date1 - date2) >= 20.0)

    //bigger
warbio
  • 466
  • 5
  • 16
1

This is a pretty simple way of doing it;

int date1 = year1 * 10000 + month1 * 100 + day1;
int date2 = year2 * 10000 + month2 * 100 + day2;
boolean greaterThan20 = (date2 - date1) >= 200000;
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
1

the Calendar object would allow you to compare dates that way.

If you instantiate two Calendar objects one with each of your dates. Then roll one of them forward / backward by 20 years.

Once that is done either the .after(), or .before() methods will tell you what you need.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • if you roll by the year you might change smaller fields and I'm not sure if this would give the right answer in all the cases. It might depend on Damir's 20 years definition... – Marsellus Wallace Feb 07 '12 at 17:13
  • Good point, my method may end up being off by some hours / days depending on any calendar weirdness due to leap year or something else. – FoamyGuy Feb 07 '12 at 17:29
1

You can use

Date(int year, int month, int date)

to get your values into a date format.

Then you can subtract one from the other and convert the value into years.

    java.util.Date lateDate, earlyDate;
    lateDate = Date(year1,month1,day1);
    earlyDate = Date(year2,month2,day2);
    deltaDays = ( lastDate.getTime() - earlyDate.getTime() )/ MILLSECS_PER_DAY;
    deltaDays = deltaDays / DAYS_IN_A_YEAR;
Jpepper
  • 437
  • 3
  • 15