From afbe2ce99c3eb5f1f897285adb1e6ed6a8815b17 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 1 Sep 2023 09:52:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DDateUtil.age=E5=B9=B4?= =?UTF-8?q?=E9=BE=84=E8=AE=A1=E7=AE=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 ++-- .../cn/hutool/core/date/CalendarUtil.java | 21 +++++++++------ .../cn/hutool/core/date/DateUtilTest.java | 26 +++++++++++++++++- .../cn/hutool/core/date/IssueI7XMYWTest.java | 27 +++++++++++++++++++ 4 files changed, 68 insertions(+), 11 deletions(-) create mode 100755 hutool-core/src/test/java/cn/hutool/core/date/IssueI7XMYWTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index bf46419b9..d91d38a88 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.22(2023-08-31) +# 5.8.22(2023-09-01) ### 🐣新特性 * 【core 】 NumberUtil.nullToZero增加重载(issue#I7PPD2@Gitee) @@ -28,7 +28,8 @@ * 【core 】 修复StrUtil#containsAny NPE问题(pr#1063@Gitee) * 【all 】 修复SONArray的add()方法抛出OutOfMemory异常问题(issue#3286@Github) * 【core 】 修复fillColumns空指针问题(issue#3284@Github) -* 【core 】 修复修复Convert不能转换Optional和Opt问题(issue#I7WJHH@Gitee) +* 【core 】 修复Convert不能转换Optional和Opt问题(issue#I7WJHH@Gitee) +* 【core 】 修复DateUtil.age年龄计算问题(issue#I7XMYW@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.8.21(2023-07-29) diff --git a/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java b/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java index 4a0f6ae91..d98cd0b3a 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java @@ -629,7 +629,12 @@ public class CalendarUtil { } /** - * 计算相对于dateToCompare的年龄,常用于计算指定生日在某年的年龄 + * 计算相对于dateToCompare的年龄,常用于计算指定生日在某年的年龄
+ * 按照《最高人民法院关于审理未成年人刑事案件具体应用法律若干问题的解释》第二条规定刑法第十七条规定的“周岁”,按照公历的年、月、日计算,从周岁生日的第二天起算。 + * * * @param birthday 生日 * @param dateToCompare 需要对比的日期 @@ -646,22 +651,22 @@ public class CalendarUtil { final int year = cal.get(Calendar.YEAR); final int month = cal.get(Calendar.MONTH); final int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); - final boolean isLastDayOfMonth = dayOfMonth == cal.getActualMaximum(Calendar.DAY_OF_MONTH); + // 复用cal cal.setTimeInMillis(birthday); int age = year - cal.get(Calendar.YEAR); - final int monthBirth = cal.get(Calendar.MONTH); - //当前日期,则为0岁 if (age == 0){ return 0; - } else if (month == monthBirth) { + } + final int monthBirth = cal.get(Calendar.MONTH); + if (month == monthBirth) { final int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); - final boolean isLastDayOfMonthBirth = dayOfMonthBirth == cal.getActualMaximum(Calendar.DAY_OF_MONTH); - if ((false == isLastDayOfMonth || false == isLastDayOfMonthBirth) && dayOfMonth <= dayOfMonthBirth) { - // 如果生日在当月,但是未超过生日当天的日期,年龄减一 + // issue#I6E6ZG,法定生日当天不算年龄,从第二天开始计算 + if (dayOfMonth <= dayOfMonthBirth) { + // 如果生日在当月,但是未达到生日当天的日期,年龄减一 age--; } } else if (month < monthBirth) { diff --git a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java index 9a9ed5a13..776615120 100755 --- a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java @@ -868,7 +868,9 @@ public class DateUtilTest { final String d1 = "2000-02-29"; final String d2 = "2018-02-28"; final int age = DateUtil.age(DateUtil.parseDate(d1), DateUtil.parseDate(d2)); - Assert.assertEquals(18, age); + + // issue#I6E6ZG,法定生日当天不算年龄,从第二天开始计算 + Assert.assertEquals(17, age); } @Test(expected = IllegalArgumentException.class) @@ -878,6 +880,28 @@ public class DateUtilTest { DateUtil.age(DateUtil.parseDate(d1), DateUtil.parseDate(d2)); } + @Test + public void ageTest3() { + // 按照《最高人民法院关于审理未成年人刑事案件具体应用法律若干问题的解释》第二条规定刑法第十七条规定的“周岁”,按照公历的年、月、日计算,从周岁生日的第二天起算。 + // 那我们认为就算当年是闰年,29日也算周岁生日的第二天,可以算作一岁 + final String d1 = "1998-02-28"; + final String d2 = "2000-02-29"; + final int age = DateUtil.age(DateUtil.parse(d1), DateUtil.parse(d2)); + // issue#I6E6ZG,法定生日当天不算年龄,从第二天开始计算 + Assert.assertEquals(2, age); + } + + @Test + public void ageTest4() { + // 按照《最高人民法院关于审理未成年人刑事案件具体应用法律若干问题的解释》第二条规定刑法第十七条规定的“周岁”,按照公历的年、月、日计算,从周岁生日的第二天起算。 + // 那我们认为就算当年是闰年,29日也算周岁生日的第二天,可以算作一岁 + final String d1 = "1999-02-28"; + final String d2 = "2000-02-29"; + final int age = DateUtil.age(DateUtil.parse(d1), DateUtil.parse(d2)); + // issue#I6E6ZG,法定生日当天不算年龄,从第二天开始计算 + Assert.assertEquals(1, age); + } + @Test public void isExpiredTest() { final DateTime startDate = DateUtil.parse("2019-12-01 17:02:30"); diff --git a/hutool-core/src/test/java/cn/hutool/core/date/IssueI7XMYWTest.java b/hutool-core/src/test/java/cn/hutool/core/date/IssueI7XMYWTest.java new file mode 100755 index 000000000..a5645f818 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/date/IssueI7XMYWTest.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package cn.hutool.core.date; + +import org.junit.Assert; +import org.junit.Test; + +public class IssueI7XMYWTest { + @Test + public void ageTest() { + DateTime date1 = DateUtil.parse("2023-08-31"); + Assert.assertEquals(49, DateUtil.age(DateUtil.parse("1973-08-31"), date1)); + + date1 = DateUtil.parse("2023-08-30"); + Assert.assertEquals(49, DateUtil.age(DateUtil.parse("1973-08-30"), date1)); + } +}