Browse Source

年龄日期格式转换

zhoutg 3 years ago
parent
commit
5e157739e2
1 changed files with 57 additions and 20 deletions
  1. 57 20
      common/src/main/java/com/diagbot/util/AgeUtil.java

+ 57 - 20
common/src/main/java/com/diagbot/util/AgeUtil.java

@@ -20,9 +20,10 @@ public class AgeUtil {
      */
      */
     public static Double convertAge(String ageStr) {
     public static Double convertAge(String ageStr) {
         try {
         try {
+            String year = "", month = "", day = "";
             // 防止程序出错
             // 防止程序出错
             if (StringUtil.isEmpty(ageStr)) {
             if (StringUtil.isEmpty(ageStr)) {
-                return 20.0;
+                return 1.0;
             }
             }
             // 数值,当成年龄处理
             // 数值,当成年龄处理
             if (isNumbers(ageStr)) {
             if (isNumbers(ageStr)) {
@@ -32,30 +33,44 @@ public class AgeUtil {
             if (ageDay(ageStr)) {
             if (ageDay(ageStr)) {
                 return getConvertDouble(ageStr, 1, 365);
                 return getConvertDouble(ageStr, 1, 365);
             }
             }
+            // X±日、X±天、X日±、X天±
+            if (ageSymbolDay(ageStr)) {
+                return getConvertDouble(ageStr, 2, 365);
+            }
             // X岁
             // X岁
             if (ageYear(ageStr)) {
             if (ageYear(ageStr)) {
                 return Double.parseDouble(ageStr.substring(0, ageStr.length() - 1));
                 return Double.parseDouble(ageStr.substring(0, ageStr.length() - 1));
             }
             }
             // 3岁7个月 | 3岁7月
             // 3岁7个月 | 3岁7月
-            if (ageSuiYue(ageStr)) {
-                String[] ageArr = new String[2];
-                int indexSui = ageStr.indexOf("岁");
-                ageArr[0] = ageStr.substring(0, indexSui);
+            if (ageYearMonth(ageStr)) {
+                int indexYear = ageStr.indexOf("岁");
+                year = ageStr.substring(0, indexYear);
                 if (ageStr.endsWith("个月")) { // 3岁7个月
                 if (ageStr.endsWith("个月")) { // 3岁7个月
-                    ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("个月"));
+                    month = ageStr.substring(indexYear + 1, ageStr.indexOf("个月"));
                 } else { // 3岁7月
                 } else { // 3岁7月
-                    ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("月"));
+                    month = ageStr.substring(indexYear + 1, ageStr.indexOf("月"));
                 }
                 }
-                return Double.parseDouble(ageArr[0]) + getHalfUp(Double.parseDouble(ageArr[1]) / 12);
+                return Double.parseDouble(year) + getHalfUp(Double.parseDouble(month) / 12);
             }
             }
             // X月|X个月
             // X月|X个月
-            if (ageYue(ageStr)) {
+            if (ageMonth(ageStr)) {
                 if (ageStr.endsWith("个月")) {
                 if (ageStr.endsWith("个月")) {
                     return getConvertDouble(ageStr, 2, 12);
                     return getConvertDouble(ageStr, 2, 12);
                 } else {
                 } else {
                     return getConvertDouble(ageStr, 1, 12);
                     return getConvertDouble(ageStr, 1, 12);
                 }
                 }
             }
             }
+            // X月Y天|X个月Y天
+            if (ageMonthDay(ageStr)) {
+                if (ageStr.contains("个月")) {
+                    month = ageStr.substring(0, ageStr.indexOf("个月"));
+                    day = ageStr.substring(ageStr.indexOf("个月") + 2, ageStr.indexOf("天"));
+                } else {
+                    month = ageStr.substring(0, ageStr.indexOf("月"));
+                    day = ageStr.substring(ageStr.indexOf("月") + 1, ageStr.indexOf("天"));
+                }
+                return getHalfUp((Double.parseDouble(month) +  (Double.parseDouble(day) / 30))/ (12));
+            }
             // X小时|时
             // X小时|时
             if (ageHour(ageStr)) {
             if (ageHour(ageStr)) {
                 if (ageStr.endsWith("小时")) {
                 if (ageStr.endsWith("小时")) {
@@ -93,9 +108,9 @@ public class AgeUtil {
             }
             }
         } catch (Exception e) {
         } catch (Exception e) {
             e.printStackTrace();
             e.printStackTrace();
-            return 20.0;
+            return 1.0;
         }
         }
-        return 20.0;
+        return 1.0;
     }
     }
 
 
     /**
     /**
@@ -132,29 +147,51 @@ public class AgeUtil {
     }
     }
 
 
     /**
     /**
-     * 判断年龄字符串:x月|x个月
+     * 判断年龄字符串:X月|X个月
      *
      *
      * @param str
      * @param str
      * @return
      * @return
      */
      */
-    public static boolean ageYue(String str) {
+    public static boolean ageMonth(String str) {
         String regex = NUMBER_REGEX + "(个)?月";
         String regex = NUMBER_REGEX + "(个)?月";
         return str.matches(regex);
         return str.matches(regex);
     }
     }
 
 
     /**
     /**
-     * 判断年龄字符串:x天|x日
+     * 判断年龄字符串:X月Y天|X个月Y天
+     *
+     * @param str
+     * @return
+     */
+    public static boolean ageMonthDay(String str) {
+        String regex = NUMBER_REGEX + "(个)?月" + NUMBER_REGEX + "天";
+        return str.matches(regex);
+    }
+
+    /**
+     * 判断年龄字符串:X天|X日
      *
      *
      * @param str
      * @param str
      * @return
      * @return
      */
      */
     public static boolean ageDay(String str) {
     public static boolean ageDay(String str) {
-        String regex = NUMBER_REGEX + "[天|日]";
+        String regex = NUMBER_REGEX + "[天日]";
+        return str.matches(regex);
+    }
+
+    /**
+     * 判断年龄字符串:X±日、X±天、X日±、X天±
+     *
+     * @param str
+     * @return
+     */
+    public static boolean ageSymbolDay(String str) {
+        String regex = NUMBER_REGEX + "[+-][天日]" + "|" +  NUMBER_REGEX + "[天日][+-]";
         return str.matches(regex);
         return str.matches(regex);
     }
     }
 
 
     /**
     /**
-     * 判断年龄字符串:x岁
+     * 判断年龄字符串:X
      *
      *
      * @param str
      * @param str
      * @return
      * @return
@@ -165,7 +202,7 @@ public class AgeUtil {
     }
     }
 
 
     /**
     /**
-     * 判断年龄字符串:x小时|时
+     * 判断年龄字符串:X小时|时
      *
      *
      * @param str
      * @param str
      * @return
      * @return
@@ -181,7 +218,7 @@ public class AgeUtil {
      * @param str
      * @param str
      * @return
      * @return
      */
      */
-    public static boolean ageSuiYue(String str) {
+    public static boolean ageYearMonth(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);
         return str.matches(regex);
     }
     }
@@ -198,7 +235,7 @@ public class AgeUtil {
     }
     }
 
 
     public static void main(String[] args) {
     public static void main(String[] args) {
-        String s = "50.天";
-        System.out.println(ageDay(s));
+        String s = "天+";
+        System.out.println(convertAge(s));
     }
     }
 }
 }