重写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了;