@JsonFormat與@DateTimeFormat注解的使用和Timestamp取出來1970問題和@JSONField(name="Timestamp",deserializeUsing= FastJsonLocalDateTimeDeserializer.class)


總結: 

  注解@JsonFormat主要是后台到前台的時間格式的轉換

  注解@DataFormAT主要是前后到后台的時間格式的轉換

@JSONField 是解決
UserDto dto = JSONObject.parseObject(strJson, UserDto.class); 解決從Json里面取出來的時間戳 為1970的問題
public class FastJsonLocalDateTimeDeserializer implements ObjectDeserializer {

    private static List<DateTimeFormatter> dateTimeFormatters = new LinkedList<>();

    static {
        // Add your own formatter to there
        dateTimeFormatters.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        dateTimeFormatters.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
        dateTimeFormatters.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"));
        dateTimeFormatters.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSS"));
    }

    @SuppressWarnings("unchecked")
    @Override
    public LocalDateTime deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        final Long input = parser.lexer.longValue();
        LocalDateTime localDateTime = null;
        localDateTime=LocalDateTime.ofEpochSecond(input,0,ZoneOffset.of("+8"));
        Assert.notNull(localDateTime, "FastJson LocalDateTime use" +
                " FastJsonTimestampDeserializer format error: " + input);
        return localDateTime;
    }

    @Override
    public int getFastMatchToken() {
        return JSONToken.LITERAL_INT;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM