解析Unix时间到Java在Android [英] parsing Unix time to java on android

查看:225
本文介绍了解析Unix时间到Java在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天。我工作的小程序,目前我正在试图解析来自JSON对象(在CouchDB的)一个Unix时间。我曾尝试多种方法,但是这一个我认为最接近。现在,我得到NumberFormatException异常:长期无效时间误差

Good day. I'm working on little application and currently I'm trying to parse a Unix time from JSON object (on CouchDB). I have tried several ways, but this one I think is closest. Now I get NumberFormatException: Invalid long: "Time" error

private Points parseJsonDocument(JSONObject json) throws JSONException, ParseException
{

    String openKey = JSON_MAP.get(MAP_OPEN);
    String closeKey = JSON_MAP.get(MAP_CLOSE);
    String highKey = JSON_MAP.get(MAP_HIGH);
    String lowKey = JSON_MAP.get(MAP_LOW);
    String volumeKey = JSON_MAP.get(MAP_VOLUME);
    String dateKey = JSON_MAP.get(MAP_DATE);


    JSONObject object = json.getJSONObject("Stock");
    JSONArray array = object.getJSONArray("Data");
//  JSONArray array = json.getJSONArray("Data");
    Points result = new Points();


    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //desired format

    Long time = Long.parseLong(dateKey);
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time *1000);



    for(int i=0;i<array.length();i++)
    {
        JSONObject obj = array.getJSONObject(i);

        Point cp = new Point();
        cp.c = obj.getDouble(closeKey);
        cp.h = obj.getDouble(highKey);
        cp.l = obj.getDouble(lowKey);
        cp.o = obj.getDouble(openKey);
        cp.v = obj.getDouble(volumeKey);

        String date = obj.getString(dateKey);
        cp.dt = df.parse(df.format(cal.getTime()));

        result.add(cp);
    }

    return result;
}

和JSON对象看起来是这样的:

and JSON object looks like this:

     {
   "_id": "abbv",
   "_rev": "12-3d250de8395a12e0c92dfb9d7d379fc4",
    "Stock": {
       "Data": [
       {
           "Time": "1386598949000",
           "Open": "50.17",
           "High": "0",
           "Low": "0",
           "Close": "51.37",
           "Volume": "7285755"
       }
]
}

这可能是一些愚蠢的错误,我只是不明白,因为缺乏经验,所以任何建议或指导将AP preciated。是的,我曾尝试阅读的javadoc,但我便无法找到解决方案。
谢谢你。

It's probably some silly mistake which I just don't see because of lack of experience, so any advice or guidance will be appreciated. And yes,I have tried reading javadocs but I could't find the solution. Thanks.

推荐答案

长时间=的Long.parseLong(da​​teKey);

正试图解析字符串时间的一个
<击>
我想你想要做的这是什么

is trying to parse the String Time as a Long. I think what you want to do is this

String timeString = array.getJSONObject(0).getString(dateKey);
Long time = Long.parseLong(timeString)

编辑:重新阅读你的code,我认为这个问题是你想太早解析您的日期

re-reading your code, I think the issue is you are trying to parse your date too early.

您需要招行长时间=的Long.parseLong(da​​teKey); 下来到您的JSON解析循环

you need to move the line Long time = Long.parseLong(dateKey); down into your JSON parsing loop.

然后,而不是长时间=的Long.parseLong(da​​teKey); ,你应该分析,而不是日期的日期的 < STRONG>键,所以的Long.parseLong(日期);

then, instead of Long time = Long.parseLong(dateKey);, you should parse the date value as opposed to the date key, so Long.parseLong(date);

这篇关于解析Unix时间到Java在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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