gpt4 book ai didi

performance - 为什么此Java代码具有此年龄验证日期比较?

转载 作者:行者123 更新时间:2023-12-03 17:21:57 26 4
gpt4 key购买 nike

Here is a responsequestion有关使用Java计算年龄的信息。

/**
* This Method is unit tested properly for very different cases ,
* taking care of Leap Year days difference in a year,
* and date cases month and Year boundary cases (12/31/1980, 01/01/1980 etc)
**/

public static int getAge(Date dateOfBirth) {

Calendar today = Calendar.getInstance();
Calendar birthDate = Calendar.getInstance();

int age = 0;

birthDate.setTime(dateOfBirth);
if (birthDate.after(today)) {
throw new IllegalArgumentException("Can't be born in the future");
}

age = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);

// If birth date is greater than todays date (after 2 days adjustment of leap year) then decrement age one year
if ( (birthDate.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR) > 3) ||
(birthDate.get(Calendar.MONTH) > today.get(Calendar.MONTH ))){
age--;

// If birth date and todays date are of same month and birth day of month is greater than todays day of month then decrement age
}else if ((birthDate.get(Calendar.MONTH) == today.get(Calendar.MONTH )) &&
(birthDate.get(Calendar.DAY_OF_MONTH) > today.get(Calendar.DAY_OF_MONTH ))){
age--;
}

return age;
}


这段代码可以正常工作,但是为什么要进行以下比较:
(birthDate.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR) > 3)

我到目前为止已经创建了一个巨大的电子表格,其中包含一年中的全天差异,以尝试查看它可能涵盖的情况,但是我看不到其他比较未涵盖的任何内容。谁能解释包括此比较的目的?以某种方式更有效吗?

最佳答案

以下来自ThreetenBP的代码示例(Java-8的反向端口)支持以下语句:不需要进行年度检查:

@Override 
public long until(Temporal endExclusive, TemporalUnit unit) {
LocalDate end = LocalDate.from(endExclusive);
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case DAYS: return daysUntil(end);
case WEEKS: return daysUntil(end) / 7;
case MONTHS: return monthsUntil(end);
case YEARS: return monthsUntil(end) / 12;
case DECADES: return monthsUntil(end) / 120;
case CENTURIES: return monthsUntil(end) / 1200;
case MILLENNIA: return monthsUntil(end) / 12000;
case ERAS: return end.getLong(ERA) - getLong(ERA);
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
}
return unit.between(this, end);
}

[...]

private long monthsUntil(LocalDate end) {
long packed1 = getProlepticMonth() * 32L + getDayOfMonth(); // no overflow
long packed2 = end.getProlepticMonth() * 32L + end.getDayOfMonth(); // no overflow
return (packed2 - packed1) / 32;
}


case YEARS: return monthsUntil(end) / 12;行(表达式 birthday.until(today, YEARS)YEARS.between(birthday, today)是等效的-一个委派给另一个)使用与OP引用的以下简化代码相同的算法,并且不引用任何年度检查:

age = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);

if (birthDate.get(Calendar.MONTH) > today.get(Calendar.MONTH)) {
age--;
}else if ((birthDate.get(Calendar.MONTH) == today.get(Calendar.MONTH )) &&
(birthDate.get(Calendar.DAY_OF_MONTH) > today.get(Calendar.DAY_OF_MONTH ))){
age--;
}




问题出现了:为什么要进行年度检查?

a)海报原本是认真对待年度想法的,后来又忘了清理

b)张贴者希望“改善”表演

以下Java-8代码演示了如果认真对待并作为完整版本,则说明基于天的算法的问题(库的选择在此无关紧要,仅算法很重要):

LocalDate birthday = LocalDate.of(2001, 3, 6);
LocalDate today = LocalDate.of(2016, 3, 5); // leap year

int age = today.getYear() - birthday.getYear();
if (birthday.getDayOfYear() > today.getDayOfYear()) {
age--;
}
System.out.println("age based on day-of-year: " + age); // 15 (wrong)
System.out.println("age based on month and day-of-month: "
+ ChronoUnit.YEARS.between(birthday, today)); // 14 (correct)


结论:

您引用的拟议的年度条款仅是噪音,因为算法的其余部分与Java-8的功能相对应。年度检查可能源自某些较早的基于年度建议代码的版本,并且尚未被清除。

为了回答您的最后一个问题:像这样不必要的检查不是好办法。就性能而言是高效的(尽管我们在这里谈论微优化)。

关于performance - 为什么此Java代码具有此年龄验证日期比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35903085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com