AgeUtil.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.diagbot.util;
  2. import java.math.BigDecimal;
  3. /**
  4. * @description: 年龄工具类
  5. * @author: zhoutg
  6. * @time: 2020/7/30 15:18
  7. */
  8. public class AgeUtil {
  9. // 数字正则,支持20;20.3;20.;.8
  10. public static final String NUMBER_REGEX = "[0-9]+|([0-9]+\\.)[0-9]*|[0-9]*(\\.[0-9]+)";
  11. /**
  12. * 年龄字符串转换成double类型
  13. *
  14. * @param ageStr
  15. * @return
  16. */
  17. public static Double convertAge(String ageStr) {
  18. try {
  19. // 防止程序出错
  20. if (StringUtil.isEmpty(ageStr)) {
  21. return 20.0;
  22. }
  23. // 数值,当成年龄处理
  24. if (isNumbers(ageStr)) {
  25. return Double.parseDouble(ageStr);
  26. }
  27. // X日|天
  28. if (ageDay(ageStr)) {
  29. return getConvertDouble(ageStr, 1, 365);
  30. }
  31. // X岁
  32. if (ageYear(ageStr)) {
  33. return Double.parseDouble(ageStr.substring(0, ageStr.length() - 1));
  34. }
  35. // 3岁7个月 | 3岁7月
  36. if (ageSuiYue(ageStr)) {
  37. String[] ageArr = new String[2];
  38. int indexSui = ageStr.indexOf("岁");
  39. ageArr[0] = ageStr.substring(0, indexSui);
  40. if (ageStr.endsWith("个月")) { // 3岁7个月
  41. ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("个月"));
  42. } else { // 3岁7月
  43. ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("月"));
  44. }
  45. return Double.parseDouble(ageArr[0]) + getHalfUp(Double.parseDouble(ageArr[1]) / 12);
  46. }
  47. // X月|X个月
  48. if (ageYue(ageStr)) {
  49. if (ageStr.endsWith("个月")) {
  50. return getConvertDouble(ageStr, 2, 12);
  51. } else {
  52. return getConvertDouble(ageStr, 1, 12);
  53. }
  54. }
  55. // X小时|时
  56. if (ageHour(ageStr)) {
  57. if (ageStr.endsWith("小时")) {
  58. return getConvertDouble(ageStr, 2, 365 * 24);
  59. } else if (ageStr.endsWith("时")) {
  60. return getConvertDouble(ageStr, 1, 365 * 24);
  61. }
  62. }
  63. // 特殊值处理
  64. switch (ageStr) {
  65. case "一岁":
  66. return 1.0;
  67. case "二岁":
  68. return 2.0;
  69. case "三岁":
  70. return 3.0;
  71. case "四岁":
  72. return 4.0;
  73. case "五岁":
  74. return 5.0;
  75. case "六岁":
  76. return 6.0;
  77. case "七岁":
  78. return 7.0;
  79. case "八岁":
  80. return 8.0;
  81. case "九岁":
  82. return 9.0;
  83. case "十岁":
  84. return 10.0;
  85. case "半时":
  86. return getHalfUp(0.5D / (365 * 24));
  87. default:
  88. break;
  89. }
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. return 20.0;
  93. }
  94. return 20.0;
  95. }
  96. /**
  97. * 获取转换后的数值
  98. *
  99. * @param ageStr 年龄字符串
  100. * @param len 字符串长度
  101. * @param denominator 分母
  102. * @return
  103. */
  104. public static Double getConvertDouble(String ageStr, int len, int denominator) {
  105. return getHalfUp(Double.parseDouble(ageStr.substring(0, ageStr.length() - len)) / denominator);
  106. }
  107. /**
  108. * 判断字符串是否 仅 包含数字
  109. *
  110. * @param str
  111. * @return
  112. */
  113. public static boolean numbersOnly(String str) {
  114. String regex = "^[0-9]+$";
  115. return str.matches(regex);
  116. }
  117. /**
  118. * 判断字符串是否数值(正)
  119. *
  120. * @param str
  121. * @return
  122. */
  123. public static boolean isNumbers(String str) {
  124. return str.matches(NUMBER_REGEX);
  125. }
  126. /**
  127. * 判断年龄字符串:x月|x个月
  128. *
  129. * @param str
  130. * @return
  131. */
  132. public static boolean ageYue(String str) {
  133. String regex = NUMBER_REGEX + "(个)?月";
  134. return str.matches(regex);
  135. }
  136. /**
  137. * 判断年龄字符串:x天|x日
  138. *
  139. * @param str
  140. * @return
  141. */
  142. public static boolean ageDay(String str) {
  143. String regex = NUMBER_REGEX + "[天|日]";
  144. return str.matches(regex);
  145. }
  146. /**
  147. * 判断年龄字符串:x岁
  148. *
  149. * @param str
  150. * @return
  151. */
  152. public static boolean ageYear(String str) {
  153. String regex = NUMBER_REGEX + "岁";
  154. return str.matches(regex);
  155. }
  156. /**
  157. * 判断年龄字符串:x小时|时
  158. *
  159. * @param str
  160. * @return
  161. */
  162. public static boolean ageHour(String str) {
  163. String regex = NUMBER_REGEX + "(小)?时";
  164. return str.matches(regex);
  165. }
  166. /**
  167. * 判断年龄字符串:3岁7个月
  168. *
  169. * @param str
  170. * @return
  171. */
  172. public static boolean ageSuiYue(String str) {
  173. String regex = "[0-9]{1,3}岁[0-9]{1,2}个{0,1}月";
  174. return str.matches(regex);
  175. }
  176. /**
  177. * 四舍五入保留2位小数
  178. *
  179. * @param ageStr
  180. * @return
  181. */
  182. public static Double getHalfUp(Double ageStr) {
  183. BigDecimal bg = new BigDecimal(String.valueOf(ageStr));
  184. return bg.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
  185. }
  186. public static void main(String[] args) {
  187. }
  188. }