gpt4 book ai didi

Java:长格式的 util.Date 对象

转载 作者:太空宇宙 更新时间:2023-11-04 09:52:01 24 4
gpt4 key购买 nike

如何返回具有长值的 Date 对象?像这样的东西:

@JsonProperty("time")
public Date getTimeInLong() {
Date date = super.getTime();
return date.getTime() //I want this value as date object
}

原因是我试图将 JSON 属性 time 覆盖为长格式。但是,如果我将 getter 签名更改为 public long getTime(){},则会收到来自

Conflicting getterDefinitions for property "time": 异常

最佳答案

您可以将带有 shape = Shape.NUMBER 参数的 @JsonFormat 注释放在现有的 getter 方法上。这是一个例子:

public class JacksonJsonFormat {
public static class Bean {
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
public Date getTime() {
return new Date();
}
}

public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Bean()));
}
}

输出:

{"time":1406068019124}

关于Java:长格式的 util.Date 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54613587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com