转换DateTime对象Java来JSON字符串 [英] Convert DateTime object java to Json string

查看:136
本文介绍了转换DateTime对象Java来JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要日期和时间对象转换在Java中的JSON字符串以如下格式:

I want to convert date time object in java to json string in format as below:

{"date":"/Date(18000000+0000)/"}

我不喜欢这一点,但它并没有给我我想要的格式为:

I did like this but it didn't give me the format i desired:

JSONObject object = new JSONObject();
object.put("date",new Date());

和object.toString的结果()

and the result of object.toString()

{"date":"Fri May 04 11:22:32 GMT+07:00 2012"}

我想字符串周五5月4日11点22分32秒GMT + 07:00 2012转变为/日期(1800万+ 0000)/(18000000 + 0000这里只是一个例子)

i want the string "Fri May 04 11:22:32 GMT+07:00 2012" transform to "/Date(18000000+0000)/" (18000000+0000 here is just a example).

感谢您的帮助。

推荐答案

这是你想要的日期格式为 /日期(小于划时代时间><时区>)/ 。结果
可以使用获得Java中的纪元时间长划时代= System.currentTimeMillis的()/ 1000; (在发现的此链接),你可以通过使用日期和时间作为格局研究以Z 获得的时区。然后所有的字符串组合成一个,并将其存储到JSON对象。结果
另一种可能性是,你从iOS设备获得的时间可能是模式的 YYMMDDHHMMSSZ 来自的此处。检查在不同时间iOS设备上输出,并确定正确的模式。

The date format that you want is /Date(<epoch time><Time Zone>)/.
You can get the epoch time in java using long epoch = System.currentTimeMillis()/1000;(found at this link) and the time zone you can get by using the date and time patteren as Z. Then combine all the strings into one and store it to the Json Object.
Other possibility is that the time you are getting from iOS device may be of the pattern yyMMddHHmmssZ as got from here. Check the output on the iOS device at different times and identify the correct pattern.

这篇关于转换DateTime对象Java来JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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