AgeUtil.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  10. * 年龄字符串转换成double类型
  11. *
  12. * @param ageStr
  13. * @return
  14. */
  15. public static Double convertAge(String ageStr) {
  16. try {
  17. // 防止程序出错
  18. if (StringUtil.isEmpty(ageStr)) {
  19. return 20.0;
  20. }
  21. // 数值,当成年龄处理
  22. if (isNumbers(ageStr)) {
  23. return Double.parseDouble(ageStr);
  24. }
  25. // 20日 | 20天
  26. if (ageStr.endsWith("日") || ageStr.endsWith("天")) {
  27. return getHalfUp((Double.parseDouble(ageStr.substring(0, ageStr.length() - 1))) / 365);
  28. }
  29. // 3岁
  30. if (ageStr.endsWith("岁")) {
  31. return Double.parseDouble(ageStr.substring(0, ageStr.length() - 1));
  32. }
  33. // 3岁7个月 | 3岁7月
  34. if (ageSuiYue(ageStr)) {
  35. String[] ageArr = new String[2];
  36. int indexSui = ageStr.indexOf("岁");
  37. ageArr[0] = ageStr.substring(0, indexSui);
  38. if (ageStr.indexOf("个月") > -1) { // 3岁7个月
  39. ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("个月"));
  40. } else { // 3岁7月
  41. ageArr[1] = ageStr.substring(indexSui + 1, ageStr.indexOf("月"));
  42. }
  43. return Double.parseDouble(ageArr[0]) + getHalfUp(Double.parseDouble(ageArr[1]) / 12);
  44. }
  45. // 1.08月 | .11月 | 3月
  46. if (ageYue(ageStr)) {
  47. String noUnit = ageStr.substring(0, ageStr.length() - 1);
  48. return getHalfUp(Double.parseDouble(noUnit) / 12);
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. return 20.0;
  53. }
  54. return 20.0;
  55. }
  56. /**
  57. * 判断字符串是否 仅 包含数字
  58. *
  59. * @param str
  60. * @return
  61. */
  62. public static boolean numbersOnly(String str) {
  63. String regex = "^[0-9]+$";
  64. return str.matches(regex);
  65. }
  66. /**
  67. * 判断字符串是否数值(正)
  68. *
  69. * @param str
  70. * @return
  71. */
  72. public static boolean isNumbers(String str) {
  73. String regex = "[0-9]+|([0-9]+\\.)+[0-9]*|[0-9]*(\\.[0-9]+)+";
  74. return str.matches(regex);
  75. }
  76. /**
  77. * 判断年龄字符串:xx月
  78. *
  79. * @param str
  80. * @return
  81. */
  82. public static boolean ageYue(String str) {
  83. String regex = "^[0-9]*[\\.]*[0-9]{1,2}月$";
  84. return str.matches(regex);
  85. }
  86. /**
  87. * 判断年龄字符串:3岁7个月
  88. *
  89. * @param str
  90. * @return
  91. */
  92. public static boolean ageSuiYue(String str) {
  93. String regex = "^[0-9]{1,3}岁[0-9]{1,2}个{0,1}月$";
  94. return str.matches(regex);
  95. }
  96. /**
  97. * 四舍五入保留2位小数
  98. *
  99. * @param ageStr
  100. * @return
  101. */
  102. public static Double getHalfUp(Double ageStr) {
  103. BigDecimal bg = new BigDecimal(String.valueOf(ageStr));
  104. return bg.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
  105. }
  106. }