java 如何使 JsonGenerator 漂亮地打印日期和日期时间值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4442966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make JsonGenerator pretty-print Date and DateTime values?
提问by ripper234
I'm using this method to convert any object to a json string:
我正在使用此方法将任何对象转换为 json 字符串:
private String objectToJson(Object object) throws IOException {
// write JSON
StringWriter writer = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
final JsonGenerator jsonGenerator = mapper.getJsonFactory().createJsonGenerator(writer);
jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());
mapper.writeValue(jsonGenerator, object);
return writer.toString();
}
When the object being printed contains fields that are java.util.Date or jodatime's DateTime, the printed value is the number of milliseconds since the epoch. I would like, instead, to pretty-print them in a standard "HH:MM:SS" notation. How should I go about doing this?
当正在打印的对象包含 java.util.Date 或 jodatime 的 DateTime 字段时,打印的值是自纪元以来的毫秒数。相反,我想用标准的“HH:MM:SS”符号漂亮地打印它们。我该怎么做呢?
采纳答案by Tim
Because the epoch timestamp (number of milliseconds since January 1st, 1970, UTC) is the most efficient and accurate representation of the time, nearly all date and time related objects are serialized to it. You can of course overwrite it and if you want to use the format mentioned in the question, you'd need to add the following to your code:
因为纪元时间戳(自 1970 年 1 月 1 日以来的毫秒数,UTC)是时间的最有效和准确的表示,几乎所有与日期和时间相关的对象都序列化到它。您当然可以覆盖它,如果您想使用问题中提到的格式,您需要将以下内容添加到您的代码中:
DateFormat myDateFormat = new SimpleDateFormat("hh:mm:ss");
objectMapper.getSerializationConfig().setDateFormat(myDateFormat);
objectMapper.getDeserializationConfig().setDateFormat(myDateFormat);
For more information regarding dates and times in the Hymanson JSON-processor see the following link: http://wiki.fasterxml.com/HymansonFAQDateHandling
有关 Hymanson JSON 处理器中日期和时间的更多信息,请参阅以下链接:http: //wiki.fasterxml.com/HymansonFAQDateHandling
回答by StaxMan
This was already mentioned in FAQ first answer points to, but just in case: you can choose between numeric and textual representation (numeric being used by default since it is much faster) by using this feature:
这已经在 FAQ first answer points to 中提到,但以防万一:您可以通过使用此功能在数字和文本表示(默认使用数字,因为它快得多)之间进行选择:
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
doing this will use the default date format, which you can then redefine as mentioned (with setDateFormat).
这样做将使用默认日期格式,然后您可以按照提到的方式重新定义(使用 setDateFormat)。
Also: you can simplify you code like so:
另外:您可以像这样简化代码:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
return mapper.writeValueAsString(object);
instead of using StringWriter and JsonGenerator explicitly.
而不是明确使用 StringWriter 和 JsonGenerator 。