有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

不尊重Java json日期格式

在我的dto中,我使用这种方法来获得db中的正确值

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone="EST")
private Date birthdate;

如果我插入2015年9月29日,我会得到正确的db值

当我显示它时,我会得到html格式的2015-09-29

为什么不使用JsonFormat

web到java是可以的,所以问题是java到web


共 (1) 个答案

  1. # 1 楼答案

    检查此链接: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));
    

    查看链接了解更多信息