public static String getFiledValueByName(String fieldName, Object object) throws Exception {
String result = null;
try {
if (StringUtils.hasText(fieldName)) {
String methodName = "";
if(StringUtils.hasText(fieldName)){
methodName = "get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
Method method = object.getClass().getMethod(methodName, new Class[] {});
Object obj = method.invoke(object, new Object[] {});
if (obj != null) {
if(obj instanceof Timestamp){
result = DateTimeUtils.toString((Timestamp)obj, DateTimeUtils.DT_FMT_YYYYMMDDHHMMSS_SLASH);
}else if(obj instanceof Date){
result = DateTimeUtils.toString((Date)obj, DateTimeUtils.DT_FMT_YYYYMMDD_SLASH);
}else{
result = obj.toString();
}
}
}
}
} catch (Exception e) {
throw e;
}
return result;
}
java 反射实例
获取对象字段值的方法
最新推荐文章于 2025-10-13 17:42:31 发布
本文介绍了一种通过反射机制获取Java对象中特定字段值的方法。该方法能够智能判断字段类型,并将其转换为字符串形式返回,特别针对Timestamp和Date类型的处理进行了优化。
172

被折叠的 条评论
为什么被折叠?



