mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复DateUtil.age年龄计算问题
This commit is contained in:
@@ -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");
|
||||
|
27
hutool-core/src/test/java/cn/hutool/core/date/IssueI7XMYWTest.java
Executable file
27
hutool-core/src/test/java/cn/hutool/core/date/IssueI7XMYWTest.java
Executable file
@@ -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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user