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());
posted @ 2018-04-03 17:06  無玑小姐  阅读(2526)  评论(0编辑  收藏  举报