java學習

          java學習

           

          BeanUtils復制對象的日期轉換

          public class Bean1 {
          private String a;
          private String b;
          private String time;
          public String getA() {
          return a;
          }
          public void setA(String a) {
          this.a = a;
          }
          public String getB() {
          return b;
          }
          public void setB(String b) {
          this.b = b;
          }
          public String getTime() {
          return time;
          }
          public void setTime(String time) {
          this.time = time;
          }
          }
          public class Bean2 {
          private int a;
          private String b;
          private Date time;
          public int getA() {
          return a;
          }
          public void setA(int a) {
          this.a = a;
          }
          public String getB() {
          return b;
          }
          public void setB(String b) {
          this.b = b;
          }
          public Date getTime() {
          return time;
          }
          public void setTime(Date time) {
          this.time = time;
          }
          }
          import java.lang.reflect.InvocationTargetException;
          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Date;
          import org.apache.commons.beanutils.BeanUtils;
          import org.apache.commons.beanutils.ConvertUtils;
          import org.apache.commons.beanutils.Converter;
          public class Test {
          public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
          Bean1 bean1 = new Bean1();
          bean1.setA("22");
          bean1.setB("fff");
          bean1.setTime("2017-01-22 11:11:11");
          Bean2 bean2 = new Bean2();
          resp();
          BeanUtils.copyProperties(bean2, bean1);
          System.out.println(bean2.getA());
          System.out.println(bean2.getB());
          System.out.println(bean2.getTime());
          }
          private static void resp() {
          ConvertUtils.register(new Converter() {
          @Override
          public Object convert(Class arg0, Object value) {
          if(value != null &&!"".equals(value.toString())){
          String v=value.toString();
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
          try {
          return sdf.parse(v);
          } catch (ParseException e) {
          e.printStackTrace();
          }
          }
          return null;
          }
          }, Date.class);
          }
          }

          posted on 2017-08-16 16:31 楊軍威 閱讀(482) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 文山县| 随州市| 石林| 肥西县| 乌兰察布市| 咸阳市| 襄汾县| 呼玛县| 潮州市| 广南县| 隆尧县| 交城县| 四平市| 公主岭市| 安义县| 长岭县| 农安县| 偃师市| 富锦市| 罗城| 平江县| 卢氏县| 乐亭县| 长宁县| 盘山县| 汉中市| 孟州市| 碌曲县| 高青县| 永登县| 德安县| 郴州市| 承德县| 临沂市| 宁明县| 榕江县| 南江县| 泸水县| 汶川县| 盐边县| 来凤县|