123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- package com.diagbot.util;
- import java.math.BigDecimal;
- /**
- * @description: 年龄工具类
- * @author: zhoutg
- * @time: 2020/7/30 15:18
- */
- 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类型
- *
- * @param ageStr
- * @return
- */
- public static Double convertAge(String ageStr) {
- try {
- // 防止程序出错
- if (StringUtil.isEmpty(ageStr)) {
- return 20.0;
- }
- // 数值,当成年龄处理
- if (isNumbers(ageStr)) {
- return Double.parseDouble(ageStr);
- }
- // X日|天
- if (ageDay(ageStr)) {
- return getConvertDouble(ageStr, 1, 365);
- }
- // 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.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);
- }
- // X月|X个月
- if (ageYue(ageStr)) {
- if (ageStr.endsWith("个月")) {
- return getConvertDouble(ageStr, 2, 12);
- } else {
- return getConvertDouble(ageStr, 1, 12);
- }
- }
- // X小时|时
- if (ageHour(ageStr)) {
- if (ageStr.endsWith("小时")) {
- return getConvertDouble(ageStr, 2, 365 * 24);
- } else if (ageStr.endsWith("时")) {
- return getConvertDouble(ageStr, 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;
- }
- return 20.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);
- }
- /**
- * 判断字符串是否 仅 包含数字
- *
- * @param str
- * @return
- */
- public static boolean numbersOnly(String str) {
- String regex = "^[0-9]+$";
- return str.matches(regex);
- }
- /**
- * 判断字符串是否数值(正)
- *
- * @param str
- * @return
- */
- public static boolean isNumbers(String str) {
- return str.matches(NUMBER_REGEX);
- }
- /**
- * 判断年龄字符串:x月|x个月
- *
- * @param str
- * @return
- */
- public static boolean ageYue(String str) {
- 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);
- }
- /**
- * 判断年龄字符串:3岁7个月
- *
- * @param str
- * @return
- */
- public static boolean ageSuiYue(String str) {
- String regex = "[0-9]{1,3}岁[0-9]{1,2}个{0,1}月";
- return str.matches(regex);
- }
- /**
- * 四舍五入保留2位小数
- *
- * @param ageStr
- * @return
- */
- public static Double getHalfUp(Double ageStr) {
- BigDecimal bg = new BigDecimal(String.valueOf(ageStr));
- return bg.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
- }
- public static void main(String[] args) {
- }
- }
|