|
@@ -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类型
|
|
|
*
|
|
@@ -17,44 +20,109 @@ public class AgeUtil {
|
|
|
*/
|
|
|
public static Double convertAge(String ageStr) {
|
|
|
try {
|
|
|
+ String year = "", month = "", day = "";
|
|
|
// 防止程序出错
|
|
|
if (StringUtil.isEmpty(ageStr)) {
|
|
|
- return 20.0;
|
|
|
+ return 1.0;
|
|
|
}
|
|
|
// 数值,当成年龄处理
|
|
|
if (isNumbers(ageStr)) {
|
|
|
return Double.parseDouble(ageStr);
|
|
|
}
|
|
|
- // 20日 | 20天
|
|
|
- if (ageStr.endsWith("日") || ageStr.endsWith("天")) {
|
|
|
- return getHalfUp((Double.parseDouble(ageStr.substring(0, ageStr.length() - 1))) / 365);
|
|
|
+ // X日|天
|
|
|
+ if (ageDay(ageStr)) {
|
|
|
+ return getConvertDouble(ageStr, 1, 365);
|
|
|
+ }
|
|
|
+ // X±日、X±天、X日±、X天±
|
|
|
+ if (ageSymbolDay(ageStr)) {
|
|
|
+ return getConvertDouble(ageStr, 2, 365);
|
|
|
}
|
|
|
- // 3岁
|
|
|
- if (ageStr.endsWith("岁")) {
|
|
|
+ // X岁
|
|
|
+ if (ageYear(ageStr)) {
|
|
|
return Double.parseDouble(ageStr.substring(0, ageStr.length() - 1));
|
|
|
}
|
|
|
// 3岁7个月 | 3岁7月
|
|
|
- if (ageSuiYue(ageStr)) {
|
|
|
- String[] ageArr = new String[2];
|
|
|
- int indexSui = ageStr.indexOf("岁");
|
|
|
- ageArr[0] = ageStr.substring(0, indexSui);
|
|
|
- if (ageStr.indexOf("个月") > -1) { // 3岁7个月
|
|
|
- ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("个月"));
|
|
|
+ if (ageYearMonth(ageStr)) {
|
|
|
+ int indexYear = ageStr.indexOf("岁");
|
|
|
+ year = ageStr.substring(0, indexYear);
|
|
|
+ if (ageStr.endsWith("个月")) { // 3岁7个月
|
|
|
+ month = ageStr.substring(indexYear + 1, ageStr.indexOf("个月"));
|
|
|
} else { // 3岁7月
|
|
|
- ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("月"));
|
|
|
+ month = ageStr.substring(indexYear + 1, ageStr.indexOf("月"));
|
|
|
+ }
|
|
|
+ return Double.parseDouble(year) + getHalfUp(Double.parseDouble(month) / 12);
|
|
|
+ }
|
|
|
+ // X月|X个月
|
|
|
+ if (ageMonth(ageStr)) {
|
|
|
+ if (ageStr.endsWith("个月")) {
|
|
|
+ return getConvertDouble(ageStr, 2, 12);
|
|
|
+ } else {
|
|
|
+ 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小时|时
|
|
|
+ if (ageHour(ageStr)) {
|
|
|
+ if (ageStr.endsWith("小时")) {
|
|
|
+ return getConvertDouble(ageStr, 2, 365 * 24);
|
|
|
+ } else if (ageStr.endsWith("时")) {
|
|
|
+ return getConvertDouble(ageStr, 1, 365 * 24);
|
|
|
}
|
|
|
- return Double.parseDouble(ageArr[0]) + getHalfUp(Double.parseDouble(ageArr[1]) / 12);
|
|
|
}
|
|
|
- // 1.08月 | .11月 | 3月
|
|
|
- if (ageYue(ageStr)) {
|
|
|
- String noUnit = ageStr.substring(0, ageStr.length() - 1);
|
|
|
- return getHalfUp(Double.parseDouble(noUnit) / 12);
|
|
|
+ // 特殊值处理
|
|
|
+ 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;
|
|
|
+ return 1.0;
|
|
|
}
|
|
|
- return 20.0;
|
|
|
+ return 1.0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取转换后的数值
|
|
|
+ *
|
|
|
+ * @param ageStr 年龄字符串
|
|
|
+ * @param len 字符串长度
|
|
|
+ * @param denominator 分母
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Double getConvertDouble(String ageStr, int len, int denominator) {
|
|
|
+ return getHalfUp(Double.parseDouble(ageStr.substring(0, ageStr.length() - len)) / denominator);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -75,18 +143,72 @@ 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(NUMBER_REGEX);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断年龄字符串:X月|X个月
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean ageMonth(String str) {
|
|
|
+ String regex = NUMBER_REGEX + "(个)?月";
|
|
|
return str.matches(regex);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断年龄字符串:xx月
|
|
|
+ * 判断年龄字符串:X月Y天|X个月Y天
|
|
|
*
|
|
|
* @param str
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean ageYue(String str) {
|
|
|
- String regex = "^[0-9]*[\\.]*[0-9]{1,2}月$";
|
|
|
+ public static boolean ageMonthDay(String str) {
|
|
|
+ String regex = NUMBER_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±日、X±天、X日±、X天±
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean ageSymbolDay(String str) {
|
|
|
+ String regex = NUMBER_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);
|
|
|
}
|
|
|
|
|
@@ -96,8 +218,8 @@ public class AgeUtil {
|
|
|
* @param str
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean ageSuiYue(String str) {
|
|
|
- String regex = "^[0-9]{1,3}岁[0-9]{1,2}个{0,1}月$";
|
|
|
+ public static boolean ageYearMonth(String str) {
|
|
|
+ String regex = "[0-9]{1,3}岁[0-9]{1,2}个{0,1}月";
|
|
|
return str.matches(regex);
|
|
|
}
|
|
|
|
|
@@ -109,6 +231,11 @@ 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();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String s = "天+";
|
|
|
+ System.out.println(convertAge(s));
|
|
|
}
|
|
|
}
|