package test;
import java.text.SimpleDateFormat;
import lombok.extern.log4j.Log4j;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
*
* @author tomyLi
*
*/
@Log4j
public class JsonUtils<T> {
public String toJsonTransferDate(T target) {
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
try {
return mapper.writeValueAsString(target);
} catch (JsonProcessingException e) {
log.error(e.getMessage());
}
return null;
}
@SuppressWarnings("unchecked")
public T toObjectTransferDate(String json, Class<?> clazz) {
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
try {
return (T) mapper.readValue(json, clazz);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
}
jackson不使用注解处理Date格式
最新推荐文章于 2025-02-20 14:23:38 发布