通过java反射机制,修改年龄字段的值
需求:将生日转为年龄
/** * 获取年龄值 */ public List getAgeInfo(List list) throws Exception { if (null == list || list.size() == 0) { return list; } Object object = list.get(0); Class<?> aClass = object.getClass(); List<Field> fieldList = new ArrayList<>() ; while (null != aClass) { fieldList.addAll(Arrays.asList(aClass .getDeclaredFields())); aClass = aClass.getSuperclass(); } if (fieldList.size() == 0){ Field field = aClass.getDeclaredField("age"); setAgeByField(object, field); }else { for (int j = 0 ; j < fieldList.size(); j++) { Field field = fieldList.get(j); if ("age".equals(field.getName())){ setAgeByField(object, field); } } } return list; } /** * 设置年龄值 * @param object * @param field * @throws Exception */ private void setAgeByField(Object object, Field field) throws Exception { if (!field.isAccessible()){ field.setAccessible(true); } String age = (String) field.get(object); age = AgeUtil.getAgeStr(DateUtil.stringToDate(DateUtil.yyyyMMddHHmmss_,age)); field.set(object,age); }
AgeUtil.getAgeStr()
@SuppressWarnings("static-access") public static String getAgeStr(Date birthDate){ String ageStr = null; if (birthDate == null) { birthDate = new Date(); } Calendar ca = Calendar.getInstance(); ca.setTime(birthDate); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); // 获取年 int year = cal.get(cal.YEAR) - ca.get(ca.YEAR); int mouth = cal.get(cal.MONTH) - ca.get(ca.MONTH); int day = cal.get(cal.DAY_OF_MONTH) - ca.get(ca.DAY_OF_MONTH); int hour = cal.get(cal.HOUR_OF_DAY) - ca.get(ca.HOUR_OF_DAY); int mouths = year * 12 + mouth; if (mouths < 12) { year = 0; } else { year = mouths / 12; } Long bDay = birthDate.getTime(); Long eDay = new Date().getTime(); int oneDay = 60 * 60 * 24 * 1000; double oneDays = (eDay - bDay) / oneDay; if (oneDays < 30) { mouth = 0; } else { mouth = mouths - year * 12; } Long bHour = birthDate.getTime(); Long eHour = new Date().getTime(); int oneHour = 60 * 60 * 1000; double oneHours = (eHour - bHour) / oneHour; if (oneHours < 24) { day = 0; } else { if (mouths == 0) { day = (int) oneDays; } } Long bm = birthDate.getTime(); Long em = new Date().getTime(); int onem = 60 * 1000; double oneMs = (em - bm) / onem; if (oneMs < 24) { hour = 0; } else { } if (year > 0) { if (mouth > 0) { ageStr = (year + "岁" + mouth + "月"); } else if (day > 0) { ageStr = (year + "岁" + day + "日"); } else if (hour > 0) { ageStr = (year + "岁" + hour + "时"); } } else if (mouth > 0) { if (day > 0) { ageStr = (mouth + "月" + day + "日"); } else if (hour > 0) { ageStr = (mouth + "月" + hour + "时"); } } else if (day > 0) { if (hour > 0) { ageStr = (day + "日" + hour + "时"); } else { day = (day * 24 + hour) / 24; ageStr = (day + "日" + (hour + 24) + "时"); } } else if (hour > 0) { ageStr = (0 + "日" + hour + "时"); } return ageStr; }
DateUtil.stringToDate()
public static Date stringToDate(String pattern, String date) throws Exception { SimpleDateFormat format = new SimpleDateFormat(pattern); try { return format.parse(date); } catch (ParseException e) { throw new BusinessException(SystemError.DATA_CONVERT_ERROR.getCode(), SystemError.DATA_CONVERT_ERROR.getMessage()); } }