简体   繁体   中英

How to calculate if the difference between two dates is greater than 20 years?

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.

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

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
}

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

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;

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM