简体   繁体   English

如何计算两个日期之间的差值是否大于20年?

[英]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? 如何计算两者之间的差额是否大于20年? I used this on registration of new users. 我在注册新用户时使用了此功能。

Use Joda-Time , period! 使用Joda-Time ,期间! :) :)

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. 尽管“大于20年”可能在某种程度上取决于您所谈论的日历类型,但是当您开始计数,leap年或夏时制时,Joda-Time将为您提供比util.Calendar类更大的灵活性。 Working with util.Date is not recommended and counting millisecond (or other things like that) will probably lead to buggy code. 不建议使用util.Date,并且计数毫秒(或类似的东西)可能会导致错误的代码。

JODA-TIME Code Samples: JODA-TIME代码示例:

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... 选项1,无聊...

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! 选项2,好东西!

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!!! 选项3,火箭科学!!! ;) ;)

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. 当然, Period类有很多方法只能获取相关字段。 Click the following for the APIs of DateTime and Days 单击以下内容以获取DateTimeDays的API

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. Calendar对象将允许您以这种方式比较日期。

If you instantiate two Calendar objects one with each of your dates. 如果实例化两个Calendar对象,则每个日期都一个。 Then roll one of them forward / backward by 20 years. 然后将其中一个向前/向后滚动20年。

Once that is done either the .after(), or .before() methods will tell you what you need. 一旦完成,.after()或.before()方法将告诉您您需要什么。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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