如何将JSON日期格式转换为简单日期格式? [英] How to Convert JSON Date Format to Simple Date Format?

查看:206
本文介绍了如何将JSON日期格式转换为简单日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以JSON格式(例如/Date(1521866513877+0530)/)接收Date的响应,而我想要的格式是这样的24/03/2018 10:11:53.

I am receiving response of Date in JSON format like /Date(1521866513877+0530)/ which I want in format like this 24/03/2018 10:11:53.

推荐答案

使用正则表达式从字符串中提取单个片段. 13位数字是自纪元以来的毫秒数.将其解析为长整数,然后将其转换为Instant(现代Java日期和时间API java.time中的类).将+0530解析为ZoneOffset;它比UTC或GMT提前5小时30分钟.将InstantZoneOffset合并为OffsetDateTime.使用DateTimeFormatter对其进行格式化.就是这样.

Use a regular expression to extract the individual pieces from the string. The 13 digit number is a count of milliseconds since the epoch. Parse it as a long and convert it to an Instant (class from java.time, the modern Java date and time API). Parse +0530 into a ZoneOffset; it’s an offset of 5 hours 30 minutes ahead of UTC or GMT. Combine the Instant and the ZoneOffset into an OffsetDateTime. Format it using a DateTimeFormatter. That’s all.

如果有些片段您仍然不知道该怎么做,请使用搜索引擎.所有这些都在Internet上的多个地方进行了解释.如果在阅读了教程和API文档并尽力而为后仍然卡住了,请在此处对Stack Overflow提出一个更具体的问题.记住要附上一份研究报告,您的最佳尝试以及失败的报告.我们将在那里提供更好的帮助.

If there are some of the pieces you still don’t know how to do, use your search engine. All of this is explained in several places on the Internet. If after reading the tutorials and the API documentation and trying your best you still get stuck, ask a much more specific question here on Stack Overflow. Remember to include a report of your research and your best attempt and how it fails. We will be able to help you much better from there.

这篇关于如何将JSON日期格式转换为简单日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆