fastjson LocalDateTime转时间戳

tvokkenx  于 2021-11-27  发布在  Java
关注(0)|答案(3)|浏览(530)

Date类型会转为时间戳,那么LocalDateTime类型怎么转为时间戳呢

public class RespData {
	private Date date;
	private LocalDateTime localDateTime;
	public RespData(Date date, LocalDateTime localDateTime) {
		super();
		this.date = date;
		this.localDateTime = localDateTime;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public LocalDateTime getLocalDateTime() {
		return localDateTime;
	}
	public void setLocalDateTime(LocalDateTime localDateTime) {
		this.localDateTime = localDateTime;
	}

}
public static void main(String[] args) {
		RespData respData = new RespData(new Date(), LocalDateTime.now());
		System.out.println(JSON.toJSONString(respData));
               //{"date":1508989350039,"localDateTime":"2017-10-26T11:42:30.212"}
	}
t3psigkw

t3psigkw1#

@KeZhimin 使用ValueFilter

bwitn5fc

bwitn5fc2#

fastJsonConfig.setSerializeFilters((ValueFilter) (o, s, source) -> {
            if (source == null) {
                return "";
            }
            if (source instanceof Date) {
                return ((Date) source).getTime();
            }
            return source;
        });

http://www.cnblogs.com/softidea/p/6619072.html

ubof19bj

ubof19bj3#

@helloworldtang thx。我回头试试一下。

相关问题