[英]JSON `date(…)` to `java.Util.Date` using `org.json`
I'm learning Java and writing an android app that consumes a JSON object that is passed by the server. 我正在学习Java并编写一个Android应用程序,它使用服务器传递的JSON对象。
I have it all working except the dates. 除了约会之外,我一切都在工作。
I get one of these back 我得到了其中一个
'SomeKey':'\/Date(1263798000000)\/'
I am using org.json.JSONObject
. 我正在使用
org.json.JSONObject
。
How do i convert SomeKey
into a java.Util.Date
? 如何将
SomeKey
转换为java.Util.Date
?
This might help: 这可能有所帮助:
public static Date JsonDateToDate(String jsonDate)
{
// "/Date(1321867151710)/"
int idx1 = jsonDate.indexOf("(");
int idx2 = jsonDate.indexOf(")");
String s = jsonDate.substring(idx1+1, idx2);
long l = Long.valueOf(s);
return new Date(l);
}
Date format is not standard in JSON, so you need to choose how you "pass it through". 日期格式在JSON中不是标准格式,因此您需要选择“如何通过”。 I think the value you are seeing is in millis.
我认为你看到的价值是毫克。
In Java: 在Java中:
System.out.println (new Date(1263798000000L));
// prints: Mon Jan 18 09:00:00 IST 2010
This is in my timezone, of course, but in any case it is a fairly recent date. 当然,这是我的时区,但无论如何,这是一个相当近的日期。
From the javadoc of the Date constructor: 从Date构造函数的javadoc:
Parameters:
参数:
date - the milliseconds since January 1, 1970, 00:00:00 GMT.date - 自1970年1月1日00:00:00 GMT以来的毫秒数。
Link to the docs here -> http://java.sun.com/javase/6/docs/api/java/util/Date.html#Date%28long%29 链接到这里的文档 - > http://java.sun.com/javase/6/docs/api/java/util/Date.html#Date%28long%29
As Yoni already mentioned, JSON does not define what a date is, or how to serialize one. 正如Yoni已经提到的,JSON没有定义日期是什么,或者如何序列化日期。 Looking at the JSON snippet you posted, it looks as if someone felt a little too creative, serializing a date like that.
看看你发布的JSON片段,看起来好像有人觉得有点太有创意,序列化这样的日期。
The important thing to note here is: to any JSON parser, this is just a string. 这里要注意的重要事项是:对于任何JSON解析器,这只是一个字符串。 The "Date(12345)" part is meaningless.
“日期(12345)”部分毫无意义。 You have to parse that yourself into a
java.util.Date
, which in that case means stripping off anything that's not a number, and using the number (the UNIX time) to instantiate a java.util.Date
. 你必须将自己解析为
java.util.Date
,在这种情况下,这意味着剥离任何不是数字的东西,并使用数字(UNIX时间)来实例化java.util.Date
。
Just for the record. 仅供记录。 A typical way to pass a date using JSON would be either
使用JSON传递日期的典型方法是
{'timestamp':1265231402}
or more likely 或者更有可能
{'timestamp':'Wed, 03 Feb 2010 22:10:38 +0100'}
The latter example would be the current timestamp (as I'm writing this) using standard RFC-2822 formatting, which can easily be parsed using Java's date utilities. 后一个例子是使用标准RFC-2822格式的当前时间戳(正如我写的那样),可以使用Java的日期实用程序轻松解析。 Have a look at SimpleDateFormat for how to parse dates in Java.
看看SimpleDateFormat如何解析Java中的日期。
public String FormartDate(String date) {
Calendar calendar = Calendar.getInstance();
String datereip = date.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(datereip);
calendar.setTimeInMillis(timeInMillis);
String DateFmtI;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
DateFmtI = simpleDateFormat.format(calendar.getTime());
return DateFmtI;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.