JSON `date(...)` 到 `java.Util.Date` 使用 `org.json`

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2195329/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:59:25  来源:igfitidea点击:

JSON `date(...)` to `java.Util.Date` using `org.json`

javaandroidjson

提问by Daniel A. White

I'm learning Java and writing an android app that consumes a JSON object that is passed by the server.

我正在学习 Java 并编写一个使用服务器传递的 JSON 对象的 android 应用程序。

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 SomeKeyinto a java.Util.Date?

我如何转换SomeKeyjava.Util.Date?

采纳答案by Yoni

Date format is not standard in JSON, so you need to choose how you "pass it through". I think the value you are seeing is in millis.

日期格式在 JSON 中不是标准格式,因此您需要选择“传递它”的方式。我认为您所看到的值以毫秒为单位。

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 以来的毫秒数。

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

回答by Eyal

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);
}

回答by Matthias

As Yoni already mentioned, JSON does not define what a date is, or how to serialize one. Looking at the JSON snippet you posted, it looks as if someone felt a little too creative, serializing a date like that.

正如 Yoni 已经提到的,JSON 没有定义日期是什么,或者如何序列化日期。看看你发布的 JSON 片段,看起来好像有人觉得有点太有创意了,像这样序列化一个日期。

The important thing to note here is: to any JSON parser, this is just a string. The "Date(12345)" part is meaningless. 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.

这里要注意的重要一点是:对于任何 JSON 解析器,这只是一个字符串。“Date(12345)”部分没有意义。您必须自己将其解析为 a java.util.Date,在这种情况下,这意味着剥离任何不是数字的内容,并使用该数字(UNIX 时间)实例化 a 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. Have a look at SimpleDateFormatfor how to parse dates in Java.

后一个示例将是使用标准 RFC-2822 格式的当前时间戳(正如我正在编写的那样),可以使用 Java 的日期实用程序轻松解析。查看SimpleDateFormat了解如何在 Java 中解析日期。

回答by TapiwaJoel Mudavanhu

    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;
    }