1、实体类属性加注解,Integer时间转时间戳,用于前端展示
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@JacksonAnnotationsInside
@JsonSerialize(using = JsonIntDateFormatSerializer.class)
public @interface JsonIntDateFormat {
}
public class JsonIntDateFormatSerializer extends JsonSerializer<Integer> {
@Override
public void serialize(Integer date, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if(null!=date){
DateTime dateTime = new DateTime(date * 1000L);
gen.writeString(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
}else {
gen.writeString("");
}
}
}
public class SecurityOpExceptionInfo{
private String uuid;// uuid
@JsonIntDateFormat
private Integer loginTime;// 登录时间
}

本文介绍了一种在Java中将Integer类型的时间戳转换为标准日期时间字符串的方法,并通过自定义的JSON序列化器实现前端友好展示。具体展示了如何使用Jackson库的@JsonSerialize注解配合自定义的JsonIntDateFormatSerializer类,将Integer类型的UNIX时间戳转换为'yyyy-MM-dd HH:mm:ss'格式的字符串。
2254

被折叠的 条评论
为什么被折叠?



