Java 根據字段名稱獲取對象字段方法和字段對應的值

//name字段名稱,clazz 對應的對象類
String name;
PropertyDescriptor pd = new PropertyDescriptor(name, clazz);
Method getMethod = pd.getReadMethod();
Object rtn = getMethod.invoke(obj);
String value = "";
// 如果是日期類型進行轉換
if (rtn != null) {
    if (rtn instanceof Date) {
        value = DateUtils.dateToString((Date)rtn);
    } else if(rtn instanceof BigDecimal){
           NumberFormat nf = new DecimalFormat("#,##0.00");                     
            value=nf.format((BigDecimal)rtn).toString();
    } else if((rtn instanceof Integer) && (Integer.valueOf(rtn.toString())<0 )){
         value="--";
            }else {
         value = rtn.toString();
             }
 }

 

發佈了35 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章