From 736fa652cac4a39fb4f2747e581a54a8122b0400 Mon Sep 17 00:00:00 2001
From: guang <guang@guang.com>
Date: 星期一, 20 三月 2023 18:38:18 +0800
Subject: [PATCH] 增加公共方法,计算年龄,字符串提取小数,map获取字符串及整数

---
 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