在我的 dto 中,我使用这种方式来获取数据库中的正确值。
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone="EST")
private Date birthdate;
如果我插入 29/09/2015,我会在 db 中获得正确的值。
当我显示它时,我得到 html 格式的 2015-09-29。
为什么不使用 JsonFormat?
web 到 java 没问题,所以问题是 java 到 web。
最佳答案
检查此链接:Jackson Serialize Dates
在你的类里面,你有这样的东西:
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
public Date eventDate;
现在,为了测试格式,您需要执行类似的操作。
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String toParse = "20-12-2014 02:30:00";
Date date = df.parse(toParse);
Event event = new Event("party", date);
ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(event);
assertThat(result, containsString(toParse));
查看链接以获取更多信息。
关于Java json 日期格式不受尊重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32851927/