From 2aad5c6ca552423d1b8e6e8c869a698a15822759 Mon Sep 17 00:00:00 2001 From: fhx <308050795@qq.com> Date: 星期二, 25 四月 2023 15:32:39 +0800 Subject: [PATCH] 1.修改redis工具 --- 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