欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

BeanUtils.copyProperties擴(kuò)展--實(shí)現(xiàn)String轉(zhuǎn)Date

 更新時(shí)間:2021年06月16日 10:43:23   作者:風(fēng)火一回  
這篇文章主要介紹了BeanUtils.copyProperties擴(kuò)展--實(shí)現(xiàn)String轉(zhuǎn)Date操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

BeanUtils.copyProperties(target,source)和PropertyUtils.copyProperties(target,source)都能將源對(duì)象的屬性的值拷貝到目標(biāo)對(duì)象相同屬性名中。

區(qū)別在于:

BeanUtils.copyProperties(target,source)

支持基礎(chǔ)類型、String、java.sql.Date、java.sql.Timestamp、java.sql.Time之間的類型轉(zhuǎn)換,即只要這些類型的屬性名相同那么拷貝就能成功。但是會(huì)默認(rèn)初始化屬性值。注意:不支持java.util.Date類型的轉(zhuǎn)化,需手動(dòng)設(shè)置。

PropertyUtils.copyProperties(target,source)

不支持類型轉(zhuǎn)換,但是不會(huì)初始話屬性值,允許屬性值為null。

在webservice中遇到了一個(gè)String類型,但是數(shù)據(jù)庫是java.util.Date類型,因?yàn)閷?duì)象屬性不較多,所以在使用PropertyUtils.copyProperties(target,source)時(shí)報(bào)錯(cuò)。

后來查了下資料,說BeanUtils能進(jìn)行類型轉(zhuǎn)換,故而就自定義了一個(gè)String轉(zhuǎn)Date的工具類。

定義工具類

package com.dhcc.phms.common.beanutils;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
public class BeanUtilsEx extends BeanUtils{
  
  static {
      //注冊(cè)u(píng)til.date的轉(zhuǎn)換器,即允許BeanUtils.copyProperties時(shí)的源目標(biāo)的util類型的值允許為空
      ConvertUtils.register(new DateConvert(), java.util.Date.class);
      ConvertUtils.register(new DateConvert(), String.class);
//      BeanUtilsBean beanUtils = new BeanUtilsBean(ConvertUtils.class,new PropertyUtilsBean());
 }
  
 public static void copyProperties(Object target, Object source) throws
        InvocationTargetException, IllegalAccessException {
      //支持對(duì)日期copy
 
   org.apache.commons.beanutils.BeanUtils.copyProperties(target, source); 
 }
}

定義日期轉(zhuǎn)換格式

package com.dhcc.phms.common.beanutils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
public class DateConvert implements Converter{
 
 @Override
 public Object convert(Class class1, Object value) {
  if(value == null){
   return null;
  }
  if(value instanceof Date){
   return value;
  }
  if (value instanceof Long) {
   Long longValue = (Long) value;
   return new Date(longValue.longValue());
  }
  if (value instanceof String) {
   String dateStr = (String)value;
   Date endTime = null;
   try {
    String regexp1 = "([0-9]{4})-([0-1][0-9])-([0-3][0-9])T([0-2][0-9]):([0-6][0-9]):([0-6][0-9])";
    String regexp2 = "([0-9]{4})-([0-1][0-9])-([0-3][0-9]) ([0-2][0-9]):([0-6][0-9]):([0-6][0-9])";
    String regexp3 = "([0-9]{4})-([0-1][0-9])-([0-3][0-9])";
    if(dateStr.matches(regexp1)){
     dateStr = dateStr.split("T")[0]+" "+dateStr.split("T")[1];
     DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     endTime = sdf.parse(dateStr);
     return endTime;
    }else if(dateStr.matches(regexp2)){
     DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     endTime = sdf.parse(dateStr);
     return endTime;
    }else if(dateStr.matches(regexp3)){
     DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     endTime = sdf.parse(dateStr);
     return endTime;
    }else{
     return dateStr;
    }
   } catch (ParseException e) {
    e.printStackTrace();
   }
  }
  return value;
 }
}

使用時(shí)用BeanUtilsEx. copyProperties(target,source)時(shí)即可實(shí)現(xiàn)String轉(zhuǎn)換為Date。

除此之外,如果需要轉(zhuǎn)換的屬性比較少時(shí),可先將source對(duì)象中沖突屬性取出來,另存一份,然后將該屬性值置為null,因?yàn)椴粫?huì)拷貝null屬性,所以拷貝的時(shí)候不會(huì)出錯(cuò)。當(dāng)拷貝完成后再將沖突屬性轉(zhuǎn)換為所需格式,set進(jìn)目標(biāo)對(duì)象。這樣也是實(shí)現(xiàn)效果。

測(cè)試代碼如下:

目標(biāo)對(duì)象TargetObject

package test;
import java.util.Date;
public class TargetObject {
 Date date;
 Boolean isOther;
 
 public TargetObject(Date date,Boolean isOther) {
  super();
  this.date = date;
  this.isOther = isOther;
 }
 
 public TargetObject() {
  super();
  // TODO Auto-generated constructor stub
 }
 
 public Date getDate() {
  return date;
 }
 
 public void setDate(Date date) {
  this.date = date;
 }
 
 public Boolean getIsOther() {
  return isOther;
 }
 
 public void setIsOther(Boolean isOther) {
  this.isOther = isOther;
 }
 
 @Override
 public String toString() {
  return "TargetObject [date=" + date + ", isOther=" + isOther + "]";
 }
 
}

源對(duì)象SourceObject

package test;
public class SourceObject {
 String date;
 String other;
 
 public SourceObject(String date,String other) {
  super();
  this.date = date;
  this.other = other;
 }
 
 public SourceObject() {
  super();
  // TODO Auto-generated constructor stub
 }
 
 public String getDate() {
  return date;
 }
 
 public void setDate(String date) {
  this.date = date;
 }
 
 public String getOther() {
  return other;
 }
 
 public void setOther(String other) {
  this.other = other;
 }
 
 @Override
 public String toString() {
  return "SourceObject [date=" + date + ", other=" + other + "]";
 } 
}

測(cè)試代碼

public static void main(String[] args) { 
     SourceObject source = new SourceObject("2017-07-17","false");
     TargetObject target = new TargetObject();
     try {
   BeanUtilsEx.copyProperties(target,source);
   System.out.println(source.toString());//SourceObject [date=2017-07-17, other=false]
   System.out.println(target.toString());//TargetObject [date=Mon Jul 17 00:00:00 CST 2017, isOther=null]
   if(source.getOther().equals("true")) {//對(duì)于屬性名不一樣的屬性是不會(huì)賦值的,需要手動(dòng)設(shè)置
    target.setIsOther(true);
   }else {
    target.setIsOther(false);
   }
  } catch (InvocationTargetException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }

BeanUtils.copyProperties 日期轉(zhuǎn)字符 日期轉(zhuǎn)Long

建立自己的日期轉(zhuǎn)換類

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang.time.DateUtils;
 
public class DateConverter implements Converter {
 private static final SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 @Override
 public Object convert(Class type, Object value) {
  if(value == null) {
         return null;
     }
     if(value instanceof Date) {
         return value;
     }
     if(value instanceof Long) {
         Long longValue = (Long) value;
         return new Date(longValue.longValue());
     }
        try {
            return dateFormat.parse(value.toString());
            //return DateUtils.parseDate(value.toString(), new String[] {"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm" });
        } catch (Exception e) {
            throw new ConversionException(e);
        }
 }
}

使用自己的日期轉(zhuǎn)換類替代默認(rèn)的。如下面的main函數(shù)

public static void main(String[] args) {
                //替換
                ConvertUtils.register(new DateConverter(), Date.class);
  //ConvertUtils.register(new StringConverter(), String.class);
  A a = new A();
  a.date="2012-03-14 17:22:16";
  B b = new B();
  try {
   BeanUtils.copyProperties(b, a);
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println(b.getDate());
 }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 微信公眾號(hào)獲取access_token的方法實(shí)例分析

    微信公眾號(hào)獲取access_token的方法實(shí)例分析

    這篇文章主要介紹了微信公眾號(hào)獲取access_token的方法,結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)微信公眾號(hào)獲取access_token的相關(guān)原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-10-10
  • MyBatis實(shí)現(xiàn)樂觀鎖和悲觀鎖的示例代碼

    MyBatis實(shí)現(xiàn)樂觀鎖和悲觀鎖的示例代碼

    在數(shù)據(jù)庫操作中,樂觀鎖和悲觀鎖是兩種常見的并發(fā)控制策略,本文主要介紹了MyBatis實(shí)現(xiàn)樂觀鎖和悲觀鎖的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • Spring?Cloud灰度部署實(shí)現(xiàn)過程詳解

    Spring?Cloud灰度部署實(shí)現(xiàn)過程詳解

    這篇文章主要為大家介紹了Spring?Cloud灰度部署實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Spring Security 中細(xì)化權(quán)限粒度的方法

    Spring Security 中細(xì)化權(quán)限粒度的方法

    這篇文章主要介紹了Spring Security 中細(xì)化權(quán)限粒度的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 詳解在Java程序中運(yùn)用Redis緩存對(duì)象的方法

    詳解在Java程序中運(yùn)用Redis緩存對(duì)象的方法

    這篇文章主要介紹了在Java程序中運(yùn)用Redis緩存對(duì)象的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 解決spring security中遇到的問題

    解決spring security中遇到的問題

    這篇文章主要介紹了解決spring security中遇到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • JAVA調(diào)用JavaScript的方法示例

    JAVA調(diào)用JavaScript的方法示例

    本文主要介紹了JAVA調(diào)用JavaScript的方法示例,主要介紹了兩種方式,一種是使用Java的ScriptEngine接口,另一種是使用Java的URLConnection類來獲取JS文件,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Java實(shí)現(xiàn)猜數(shù)字小游戲(有次數(shù)限制)

    Java實(shí)現(xiàn)猜數(shù)字小游戲(有次數(shù)限制)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)猜數(shù)字小游戲,有次數(shù)限制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Java中RocketMq的消費(fèi)方式詳解

    Java中RocketMq的消費(fèi)方式詳解

    這篇文章主要介紹了Java中RocketMq的消費(fèi)方式詳解,RocketMQ的消費(fèi)方式都是基于拉模式拉取消息的,而在這其中有一種長(zhǎng)輪詢機(jī)制(對(duì)普通輪詢的一種優(yōu)化),來平衡上面Push/Pull模型的各自缺點(diǎn),需要的朋友可以參考下
    2023-10-10
  • java分頁之假分頁實(shí)現(xiàn)簡(jiǎn)單的分頁器

    java分頁之假分頁實(shí)現(xiàn)簡(jiǎn)單的分頁器

    這篇文章主要介紹了java分頁之假分頁實(shí)現(xiàn)簡(jiǎn)單的分頁器的相關(guān)資料,需要的朋友可以參考下
    2016-04-04

最新評(píng)論