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)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 台江县| 车险| 林芝县| 松溪县| 宁都县| 扎赉特旗| 凤山市| 新丰县| 斗六市| 尚志市| 八宿县| 武清区| 瑞金市| 进贤县| 福安市| 临沭县| 邵阳县| 威海市| 阜南县| 房产| 侯马市| 奈曼旗| 彭阳县| 抚州市| 垣曲县| 中西区| 社旗县| 临汾市| 英山县| 灌云县| 望谟县| 微博| 南安市| 南木林县| 丰城市| 莒南县| 常宁市| 沅陵县| 石棉县| 广南县| 高要市|