From c1a9eee9c9aa0d3c7592fa75e70243e0297b3a0d Mon Sep 17 00:00:00 2001 From: fhx <308050795@qq.com> Date: 星期四, 06 四月 2023 10:38:03 +0800 Subject: [PATCH] 1.新增文件工具类 --- src/main/java/com/hx/util/DateUtil.java | 33 +++++++++++++++++++++++++++++++++ 1 files changed, 33 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/util/DateUtil.java b/src/main/java/com/hx/util/DateUtil.java index ddefebf..361a86c 100644 --- a/src/main/java/com/hx/util/DateUtil.java +++ b/src/main/java/com/hx/util/DateUtil.java @@ -780,4 +780,37 @@ calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return calendar.getTime(); } + + /** + * 鏍规嵁鍑虹敓骞存湀鏃ヨ绠楀勾榫� + * @param birth + * @return + */ + public static int getAge(Date birth) { + Calendar cal = Calendar.getInstance(); + int thisYear = cal.get(Calendar.YEAR); + int thisMonth = cal.get(Calendar.MONTH); + int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); + + cal.setTime(birth); + int birthYear = cal.get(Calendar.YEAR); + int birthMonth = cal.get(Calendar.MONTH); + int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH); + + int age = thisYear - birthYear; + + // 鏈冻鏈� + if (thisMonth <= birthMonth) { + // 褰撴湀 + if (thisMonth == birthMonth) { + // 鏈冻鏃� + if (dayOfMonth < birthdayOfMonth) { + age--; + } + } else { + age--; + } + } + return age; + } } -- Gitblit v1.8.0