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/NumberUtil.java |   32 ++++++++++++++++
 src/main/java/com/hx/util/DateUtil.java   |   33 ++++++++++++++++
 src/main/java/com/hx/util/MapUtil.java    |   44 ++++++++++++++++++++++
 3 files changed, 109 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;
+    }
 }
diff --git a/src/main/java/com/hx/util/MapUtil.java b/src/main/java/com/hx/util/MapUtil.java
new file mode 100644
index 0000000..81ad5a6
--- /dev/null
+++ b/src/main/java/com/hx/util/MapUtil.java
@@ -0,0 +1,44 @@
+package com.hx.util;
+
+import java.util.Map;
+
+/**
+ * map宸ュ叿绫�
+ *
+ * @author cmg
+ * @date 2023-03-16
+ */
+public class MapUtil {
+
+    /**
+     * 浠巑ap涓幏鍙�
+     * @param map
+     * @param key
+     * @return
+     */
+    public static String getString(Map<String, Object> map, String key)
+    {
+        if(map == null || StringUtils.isEmpty(key))
+        {
+            return null;
+        }
+
+        return null == map.get(key) ? null : map.get(key).toString();
+    }
+
+    /**
+     * 浠巑ap涓幏鍙栨暣鏁帮紝濡傛灉娌℃湁锛屽垯杩斿洖-1
+     * @param map
+     * @param key
+     * @return
+     */
+    public static int getInt(Map<String, Object> map, String key)
+    {
+        if(map == null || StringUtils.isEmpty(key))
+        {
+            return -1;
+        }
+
+        return null == map.get(key) ? -1 : Integer.parseInt(map.get(key).toString());
+    }
+}
diff --git a/src/main/java/com/hx/util/NumberUtil.java b/src/main/java/com/hx/util/NumberUtil.java
index 27d25e0..59b9581 100644
--- a/src/main/java/com/hx/util/NumberUtil.java
+++ b/src/main/java/com/hx/util/NumberUtil.java
@@ -3,6 +3,8 @@
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.DecimalFormat;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * 鏁板瓧宸ュ叿
@@ -351,4 +353,34 @@
         return result2;
     }
 
+    /**
+     * 浠庡瓧绗︿覆涓彁鍙栧皬鏁�
+     * @param str
+     * @return
+     */
+    public static String getNumber(String str){
+        // 鎺у埗姝e垯琛ㄨ揪寮忕殑鍖归厤琛屼负鐨勫弬鏁�(灏忔暟)
+        Pattern p = Pattern.compile("(\\d+\\.\\d+)");
+        //Matcher绫荤殑鏋勯�犳柟娉曚篃鏄鏈夌殑,涓嶈兘闅忔剰鍒涘缓,鍙兘閫氳繃Pattern.matcher(CharSequence input)鏂规硶寰楀埌璇ョ被鐨勫疄渚�.
+        Matcher m = p.matcher(str);
+        //m.find鐢ㄦ潵鍒ゆ柇璇ュ瓧绗︿覆涓槸鍚﹀惈鏈変笌"(\\d+\\.\\d+)"鐩稿尮閰嶇殑瀛愪覆
+        if (m.find()) {
+            //濡傛灉鏈夌浉鍖归厤鐨�,鍒欏垽鏂槸鍚︿负null鎿嶄綔
+            //group()涓殑鍙傛暟锛�0琛ㄧず鍖归厤鏁翠釜姝e垯锛�1琛ㄧず鍖归厤绗竴涓嫭鍙风殑姝e垯,2琛ㄧず鍖归厤绗簩涓鍒�,鍦ㄨ繖鍙湁涓�涓嫭鍙�,鍗�1鍜�0鏄竴鏍风殑
+            str = m.group(1) == null ? "" : m.group(1);
+        } else {
+            //濡傛灉鍖归厤涓嶅埌灏忔暟锛屽氨杩涜鏁存暟鍖归厤
+            p = Pattern.compile("(\\d+)");
+            m = p.matcher(str);
+            if (m.find()) {
+                //濡傛灉鏈夋暣鏁扮浉鍖归厤
+                str = m.group(1) == null ? "" : m.group(1);
+            } else {
+                //濡傛灉娌℃湁灏忔暟鍜屾暣鏁扮浉鍖归厤,鍗冲瓧绗︿覆涓病鏈夋暣鏁板拰灏忔暟锛屽氨璁句负绌�
+                str = "";
+            }
+        }
+        return str;
+    }
+
 }

--
Gitblit v1.8.0