json 日期格式转换
json 日期格式转换
1.bean中属性getter增加格式化注释
private Date upTime;
@JsonSerialize(using=JsonDateSerializer.class)
public Date getUpTime() {
return upTime;
}
public void setUpTime(Date upTime) {
this.upTime = upTime;
}
2.定义格式化类
public class JsonDateSerializer extends JsonSerializer<Date> {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@Override
public void serialize(Date arg0, JsonGenerator arg1, SerializerProvider arg2)
throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
String formattedDate = dateFormat.format(arg0);
arg1.writeString(formattedDate);
}
}