简体   繁体   English

如何计算2个日期之间的年数差异?

[英]How do I calculate the number of years difference between 2 dates?

如何计算Java中2个日历之间的年份差异?

First you need to determine which one is older than the other. 首先,您需要确定哪一个比另一个更旧。 Then make use of a while loop wherein you test if the older one isn't after() the newer one. 然后使用while循环,在其中测试较旧的循环是否after()较新的循环after() Invoke Calendar#add() with one ( 1 ) Calendar.YEAR on the older one to add the years. 调用Calendar#add() ,在旧版本上添加一( 1 )个Calendar.YEAR来添加年份。 Keep a counter to count the years. 保持柜台计算年数。

Kickoff example: 开球示例:

Calendar myBirthDate = Calendar.getInstance();
myBirthDate.clear();
myBirthDate.set(1978, 3 - 1, 26);
Calendar now = Calendar.getInstance();
Calendar clone = (Calendar) myBirthDate.clone(); // Otherwise changes are been reflected.
int years = -1;
while (!clone.after(now)) {
    clone.add(Calendar.YEAR, 1);
    years++;
}
System.out.println(years); // 32

That said, the Date and Calendar API's in Java SE are actually epic failures. 也就是说,Java SE中的DateCalendar API实际上是史诗般的失败。 There's a new Date API in planning for upcoming Java 8, the JSR-310 which is much similar to Joda-Time . 在规划即将推出的Java 8时,有一个新的Date API, JSR-310Joda-Time非常相似。 As far now you may want to consider Joda-Time since it really eases Date/Time calculations/modifications like this. 到目前为止,你可能想要考虑Joda-Time,因为它确实简化了这样的日期/时间计算/修改。 Here's an example using Joda-Time: 以下是使用Joda-Time的示例:

DateTime myBirthDate = new DateTime(1978, 3, 26, 0, 0, 0, 0);
DateTime now = new DateTime();
Period period = new Period(myBirthDate, now);
int years = period.getYears();
System.out.println(years); // 32

Much more clear and concise, isn't it? 更清晰简洁,不是吗?

Calendar dobDate; // Set this to date to check
Calendar today = Calendar.getInstance();
int curYear = today.get(Calendar.YEAR);
int curMonth = today.get(Calendar.MONTH);
int curDay = today.get(Calendar.DAY_OF_MONTH);

int year = dobDate.get(Calendar.YEAR);
int month = dobDate.get(Calendar.MONTH);
int day = dobDate.get(Calendar.DAY_OF_MONTH);

int age = curYear - year;
if (curMonth < month || (month == curMonth && curDay < day)) {
    age--;
}

This avoids looping and should be accurate to the day. 这样可以避免循环,并且应该精确到当天。

double diff = d2.getTime() - d1.getTime();
double d = 1000 * 60 * 60 * 24 * 365;
return (int) Math.round(diff / d);

(java.lang.Math) (java.lang.Math中)

Or you could just use Calendar.getTimeInMillis() : 或者你可以使用Calendar.getTimeInMillis()

long msYear = 1000L * 60 * 60 * 24 * 365;
long msDiff = c2.getTimeInMillis() - c1.getTimeInMillis();
System.out.println("Years diff: " + String.valueOf(msDiff / msYear));

Edit This ignores leap years. 编辑这忽略了闰年。 But if you are looking for an integer representation of the number of years the leap years are irrelevant for periods shorter than half a millennium or so. 但是,如果您正在寻找年数的整数表示,闰年与短于半个千年的时期无关。

This is plenty of exactness for calculating somebody's age, for example. 例如,这对计算某人的年龄非常准确。 But not accurate enough to tell their birthday. 但不够准确,无法告诉他们的生日。

If you need your year difference more exact than to three significant digits within a human lifespan (approximately 80.00 years vs 80.05), you should use the "official" solution. 如果您需要的年份差异比人类寿命中的三位有效数字(大约80.00岁对80.05)更精确,那么您应该使用“官方”解决方案。

If not, there's no particular reason to put in extra effort for a degree of precision you are not going to use. 如果没有,没有特别的理由为你不会使用的精确度付出额外的努力。

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

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