Chr*_*ssy 6 java json iso8601 datetime-format jackson
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
objectMapper.setDateFormat(new ISO8601DateFormat());
Run Code Online (Sandbox Code Playgroud)
很好,但这忽略了毫秒,如何在不使用非线程安全的情况下在日期中获取它们SimpleDateFormatter?
小智 7
ISO8601DateFormat.format调用ISO8601Utils.format(date),反过来调用format(date, false, TIMEZONE_Z)- 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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以在对象映射器中使用这个新类:
ObjectMapper objectMapper = new ObjectMapper();
ISO8601DateFormat dateFormat = new ISO8601WithMillisFormat();
objectMapper.setDateFormat(dateFormat);
Run Code Online (Sandbox Code Playgroud)
我做了一个测试,new Date()结果是2017-07-24T12:14:26.817Z(用毫秒).
| 归档时间: |
|
| 查看次数: |
2083 次 |
| 最近记录: |