net.sf.json.JSONObject 转实体类时遇到 空“”被赋值一个当前时间的问题,解决

重写DateMorpherEx 类中的setDefaultValue(Date defaultValue)这个方法


public void setDefaultValue(Date defaultValue){
          if(defaultValue!=null)
              this.defaultValue = ((Date)defaultValue.clone());
          else
              this.defaultValue = null;
}

新建一个DateMorpherEx 类,继承AbstractObjectMorpher

package com.macrosan.sems.util.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import net.sf.ezmorph.MorphException;
import net.sf.ezmorph.object.AbstractObjectMorpher;

/**
 *
 *
 * @author 王彦宝 
 * @date 创建时间:2017年12月10日 下午5:39:53
 * @version 1.0
 */
public class DateMorpherEx extends AbstractObjectMorpher{


    private Date defaultValue;
      private String[] formats;
      private boolean lenient;
      private Locale locale;

      public DateMorpherEx(String[] formats){
        this(formats, Locale.getDefault(), false);
      }

      public DateMorpherEx(String[] formats, boolean lenient){
        this(formats, Locale.getDefault(), lenient);
      }

      public DateMorpherEx(String[] formats, Date defaultValue){
        this(formats, defaultValue, Locale.getDefault(), false);
      }

      public DateMorpherEx(String[] formats, Date defaultValue, Locale locale, boolean lenient){
        super(true);
        if ((formats == null) || (formats.length == 0)) {
          throw new MorphException("invalid array of formats");
        }

        this.formats = formats;

        if (locale == null)
          this.locale = Locale.getDefault();
        else {
          this.locale = locale;
        }

        this.lenient = lenient;
        setDefaultValue(defaultValue);
      }

      public DateMorpherEx(String[] formats, Locale locale){
        this(formats, locale, false);
      }

      public DateMorpherEx(String[] formats, Locale locale, boolean lenient){
        if ((formats == null) || (formats.length == 0)) {
          throw new MorphException("invalid array of formats");
        }

        this.formats = formats;

        if (locale == null)
          this.locale = Locale.getDefault();
        else {
          this.locale = locale;
        }

        this.lenient = lenient;
      }

      public boolean equals(Object obj){
        if (this == obj) {
          return true;
        }
        if (obj == null) {
          return false;
        }

        if (!(obj instanceof DateMorpherEx)) {
          return false;
        }

        DateMorpherEx other = (DateMorpherEx)obj;
        EqualsBuilder builder = new EqualsBuilder();
        builder.append(this.formats, other.formats);
        builder.append(this.locale, other.locale);
        builder.append(this.lenient, other.lenient);
        if ((super.isUseDefault()) && (other.isUseDefault())) {
          builder.append(getDefaultValue(), other.getDefaultValue());
          return builder.isEquals();
        }if ((!super.isUseDefault()) && (!other.isUseDefault())) {
          return builder.isEquals();
        }
        return false;
      }

      public Date getDefaultValue() {
          if(this.defaultValue!=null)
              return (Date)this.defaultValue.clone();
          else
              return this.defaultValue;
      }

      public int hashCode(){
        HashCodeBuilder builder = new HashCodeBuilder();
        builder.append(this.formats);
        builder.append(this.locale);
        builder.append(this.lenient);
        if (super.isUseDefault()) {
          builder.append(getDefaultValue());
        }
        return builder.toHashCode();
      }

      public Object morph(Object value){
        if (value == null) {
          return null;
        }

        if (Date.class.isAssignableFrom(value.getClass())) {
          return (Date)value;
        }

        if (!supports(value.getClass())) {
          throw new MorphException(value.getClass() + " is not supported");
        }

        String strValue = (String)value;
        SimpleDateFormat dateParser = null;

        for (int i = 0; i < this.formats.length; ++i) {
          if (dateParser == null)
            dateParser = new SimpleDateFormat(this.formats[i], this.locale);
          else {
            dateParser.applyPattern(this.formats[i]);
          }
          dateParser.setLenient(this.lenient);
          try {
            return dateParser.parse(strValue.toLowerCase());
          }
          catch (ParseException localParseException)
          {
          }

        }

        if (super.isUseDefault()) {
          return this.defaultValue;
        }
        throw new MorphException("Unable to parse the date " + value);
      }

      public Class morphsTo() {
        return Date.class;
      }

      public void setDefaultValue(Date defaultValue){
          if(defaultValue!=null)
              this.defaultValue = ((Date)defaultValue.clone());
          else
              this.defaultValue = null;
      }

      public boolean supports(Class clazz){
        return String.class.isAssignableFrom(clazz);
      }

}

在自己需要转实体类的代码之前加上下面这个话就可以了

 JSONUtils.getMorpherRegistry()
         .registerMorpher(
          new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss",
	                        "yyyy-MM-dd", "yyyy-MM-dd't'HH:mm:ss" },
                             (Date) null));//调用DateMorpherEx,defaultValue为null

ok了;

转载于:https://my.oschina.net/u/3267498/blog/1595681

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>