JSON toBean Timestamp To Date 时间戳转日期

时间:2023-03-09 06:04:53
JSON toBean Timestamp To Date 时间戳转日期

时间戳格式的时间从json转为date时

配置:

import java.util.Date;

import net.sf.ezmorph.object.AbstractObjectMorpher;
/**
*
* @author chaico
*
*/
public class TimestampToDateMorpher extends AbstractObjectMorpher { public Object morph(Object value) {
if( value != null){
return new Date(Long.parseLong(String.valueOf(value)));
}
return null;
} @Override
public Class morphsTo() {
return Date.class;
} public boolean supports( Class clazz ){
return Long.class.isAssignableFrom( clazz );
}
}

在JSON toBean时调用:

JSONUtils.getMorpherRegistry().registerMorpher(new TimestampToDateMorpher());
JSONObject jsonObject = JSONObject.fromObject(responseJson);
JSONObject.toBean(jsonObject, this.getClass());