增加公共方法,计算年龄,字符串提取小数,map获取字符串及整数
| | |
| | | calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 根据出生年月日计算年龄 |
| | | * @param birth |
| | | * @return |
| | | */ |
| | | public static int getAge(Date birth) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | int thisYear = cal.get(Calendar.YEAR); |
| | | int thisMonth = cal.get(Calendar.MONTH); |
| | | int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); |
| | | |
| | | cal.setTime(birth); |
| | | int birthYear = cal.get(Calendar.YEAR); |
| | | int birthMonth = cal.get(Calendar.MONTH); |
| | | int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH); |
| | | |
| | | int age = thisYear - birthYear; |
| | | |
| | | // 未足月 |
| | | if (thisMonth <= birthMonth) { |
| | | // 当月 |
| | | if (thisMonth == birthMonth) { |
| | | // 未足日 |
| | | if (dayOfMonth < birthdayOfMonth) { |
| | | age--; |
| | | } |
| | | } else { |
| | | age--; |
| | | } |
| | | } |
| | | return age; |
| | | } |
| | | } |
New file |
| | |
| | | package com.hx.util; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * map工具类 |
| | | * |
| | | * @author cmg |
| | | * @date 2023-03-16 |
| | | */ |
| | | public class MapUtil { |
| | | |
| | | /** |
| | | * 从map中获取 |
| | | * @param map |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public static String getString(Map<String, Object> map, String key) |
| | | { |
| | | if(map == null || StringUtils.isEmpty(key)) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | return null == map.get(key) ? null : map.get(key).toString(); |
| | | } |
| | | |
| | | /** |
| | | * 从map中获取整数,如果没有,则返回-1 |
| | | * @param map |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public static int getInt(Map<String, Object> map, String key) |
| | | { |
| | | if(map == null || StringUtils.isEmpty(key)) |
| | | { |
| | | return -1; |
| | | } |
| | | |
| | | return null == map.get(key) ? -1 : Integer.parseInt(map.get(key).toString()); |
| | | } |
| | | } |
| | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.text.DecimalFormat; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * 数字工具 |
| | |
| | | return result2; |
| | | } |
| | | |
| | | /** |
| | | * 从字符串中提取小数 |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static String getNumber(String str){ |
| | | // 控制正则表达式的匹配行为的参数(小数) |
| | | Pattern p = Pattern.compile("(\\d+\\.\\d+)"); |
| | | //Matcher类的构造方法也是私有的,不能随意创建,只能通过Pattern.matcher(CharSequence input)方法得到该类的实例. |
| | | Matcher m = p.matcher(str); |
| | | //m.find用来判断该字符串中是否含有与"(\\d+\\.\\d+)"相匹配的子串 |
| | | if (m.find()) { |
| | | //如果有相匹配的,则判断是否为null操作 |
| | | //group()中的参数:0表示匹配整个正则,1表示匹配第一个括号的正则,2表示匹配第二个正则,在这只有一个括号,即1和0是一样的 |
| | | str = m.group(1) == null ? "" : m.group(1); |
| | | } else { |
| | | //如果匹配不到小数,就进行整数匹配 |
| | | p = Pattern.compile("(\\d+)"); |
| | | m = p.matcher(str); |
| | | if (m.find()) { |
| | | //如果有整数相匹配 |
| | | str = m.group(1) == null ? "" : m.group(1); |
| | | } else { |
| | | //如果没有小数和整数相匹配,即字符串中没有整数和小数,就设为空 |
| | | str = ""; |
| | | } |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | } |