Pārlūkot izejas kodu

年龄工具类

zhoutg 3 gadi atpakaļ
vecāks
revīzija
84bf035d6e
1 mainītis faili ar 84 papildinājumiem un 13 dzēšanām
  1. 84 13
      common/src/main/java/com/diagbot/util/AgeUtil.java

+ 84 - 13
common/src/main/java/com/diagbot/util/AgeUtil.java

@@ -9,6 +9,9 @@ import java.math.BigDecimal;
  */
 public class AgeUtil {
 
+    // 数字正则,支持20;20.3;20.;.8
+    public static final String NUMBER_REGEX = "[0-9]+|([0-9]+\\.)[0-9]*|[0-9]*(\\.[0-9]+)";
+
     /**
      * 年龄字符串转换成double类型
      *
@@ -25,12 +28,12 @@ public class AgeUtil {
             if (isNumbers(ageStr)) {
                 return Double.parseDouble(ageStr);
             }
-            // 20日 | 20
-            if (ageStr.endsWith("日") || ageStr.endsWith("天")) {
+            // X日|
+            if (ageDay(ageStr)) {
                 return getHalfUp((Double.parseDouble(ageStr.substring(0, ageStr.length() - 1))) / 365);
             }
-            // 3
-            if (ageStr.endsWith("岁")) {
+            // X
+            if (ageYear(ageStr)) {
                 return Double.parseDouble(ageStr.substring(0, ageStr.length() - 1));
             }
             // 3岁7个月 | 3岁7月
@@ -38,18 +41,54 @@ public class AgeUtil {
                 String[] ageArr = new String[2];
                 int indexSui = ageStr.indexOf("岁");
                 ageArr[0] = ageStr.substring(0, indexSui);
-                if (ageStr.indexOf("个月") > -1) { // 3岁7个月
+                if (ageStr.endsWith("个月")) { // 3岁7个月
                     ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("个月"));
                 } else { // 3岁7月
                     ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("月"));
                 }
                 return Double.parseDouble(ageArr[0]) + getHalfUp(Double.parseDouble(ageArr[1]) / 12);
             }
-            // 1.08月 | .11月 | 3
+            // X月 | X个
             if (ageYue(ageStr)) {
-                String noUnit = ageStr.substring(0, ageStr.length() - 1);
+                String noUnit = "";
+                if (ageStr.endsWith("个月")) {
+                    noUnit = ageStr.substring(0, ageStr.length() - 2);
+                } else {
+                    noUnit = ageStr.substring(0, ageStr.length() - 1);
+                }
                 return getHalfUp(Double.parseDouble(noUnit) / 12);
             }
+            // X时
+            if (ageHour(ageStr)) {
+                return getHalfUp((Double.parseDouble(ageStr.substring(0, ageStr.length() - 1))) / (365 * 24));
+            }
+            // 特殊值处理
+            switch (ageStr) {
+                case "一岁":
+                    return 1.0;
+                case "二岁":
+                    return 2.0;
+                case "三岁":
+                    return 3.0;
+                case "四岁":
+                    return 4.0;
+                case "五岁":
+                    return 5.0;
+                case "六岁":
+                    return 6.0;
+                case "七岁":
+                    return 7.0;
+                case "八岁":
+                    return 8.0;
+                case "九岁":
+                    return 9.0;
+                case "十岁":
+                    return 10.0;
+                case "半时":
+                    return getHalfUp(0.5D / (365 * 24));
+                default:
+                    break;
+            }
         } catch (Exception e) {
             e.printStackTrace();
             return 20.0;
@@ -75,18 +114,50 @@ public class AgeUtil {
      * @return
      */
     public static boolean isNumbers(String str) {
-        String regex = "[0-9]+|([0-9]+\\.)+[0-9]*|[0-9]*(\\.[0-9]+)+";
-        return str.matches(regex);
+        return str.matches(NUMBER_REGEX);
     }
 
     /**
-     * 判断年龄字符串:xx月
+     * 判断年龄字符串:x月|x
      *
      * @param str
      * @return
      */
     public static boolean ageYue(String str) {
-        String regex = "^[0-9]*[\\.]*[0-9]{1,2}月$";
+        String regex = NUMBER_REGEX + "(个)?月";
+        return str.matches(regex);
+    }
+
+    /**
+     * 判断年龄字符串:x天|x日
+     *
+     * @param str
+     * @return
+     */
+    public static boolean ageDay(String str) {
+        String regex = NUMBER_REGEX + "[天|日]";
+        return str.matches(regex);
+    }
+
+    /**
+     * 判断年龄字符串:x岁
+     *
+     * @param str
+     * @return
+     */
+    public static boolean ageYear(String str) {
+        String regex = NUMBER_REGEX + "岁";
+        return str.matches(regex);
+    }
+
+    /**
+     * 判断年龄字符串:x时
+     *
+     * @param str
+     * @return
+     */
+    public static boolean ageHour(String str) {
+        String regex = NUMBER_REGEX + "时";
         return str.matches(regex);
     }
 
@@ -97,7 +168,7 @@ public class AgeUtil {
      * @return
      */
     public static boolean ageSuiYue(String str) {
-        String regex = "^[0-9]{1,3}岁[0-9]{1,2}个{0,1}月$";
+        String regex = "[0-9]{1,3}岁[0-9]{1,2}个{0,1}月";
         return str.matches(regex);
     }
 
@@ -109,6 +180,6 @@ public class AgeUtil {
      */
     public static Double getHalfUp(Double ageStr) {
         BigDecimal bg = new BigDecimal(String.valueOf(ageStr));
-        return bg.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
+        return bg.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
     }
 }