有 Java 编程相关的问题?

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


共 (1) 个答案

  1. # 1 楼答案

    ISO8601DateFormat.format调用ISO8601Utils.format(date),后者反过来调用^{},-false参数告诉jackson不要包含毫秒

    似乎无法配置此类或设置任何参数,但幸运的是,它可以扩展:

    public class ISO8601WithMillisFormat extends ISO8601DateFormat {
        @Override
        public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
            String value = ISO8601Utils.format(date, true); // "true" to include milliseconds
            toAppendTo.append(value);
            return toAppendTo;
        }
    }
    

    然后我们可以在对象映射器中使用这个新类:

    ObjectMapper objectMapper = new ObjectMapper();
    ISO8601DateFormat dateFormat = new ISO8601WithMillisFormat();
    objectMapper.setDateFormat(dateFormat);
    

    我用new Date()做了一个测试,结果是2017-07-24T12:14:26.817Z(毫秒)