1

我从服务器获取具有这种格式的 Json 对象,"Date": "/Date(1337032800000+0000)/"我需要它像“1 April”。

我试图像这样解析它,但我总是得到"Unparseable date" message

mDate = jsonArray.getJSONObject(i).getString("Date");
                    String shortDate = mDate.substring(6, mDate.length()-7);
                    String format="yyyy-MM-dd";
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    try {
                        Date date = sdf.parse(shortDate);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

提前非常感谢!

4

2 回答 2

2

这是时间戳?为什么您不将 String 更改为 long 并且:

Date date = new Date();
date.setTime(yourLongVariable);

月:

    int month = date.getMonth();

    months = " " + res.getString(R.string.january);
    if(month == 2)
    {
        months = " " + res.getString(R.string.febuary);
    }
    else if(month == 3)
    {
        months = " " + res.getString(R.string.march);
    }
    else if(month == 4)
    {
        months = " " + res.getString(R.string.april);
    }
    else if(month == 5)
    {
        months = " " + res.getString(R.string.may);
    }
    else if(month == 6)
    {
        months = " " + res.getString(R.string.june);
    }
    else if(month == 7)
    {
        months = " " + res.getString(R.string.july);
    }
    else if(month == 8)
    {
        months = " " + res.getString(R.string.august);
    }
    else if(month == 9)
    {
        months = " " + res.getString(R.string.september);
    }
    else if(month == 10)
    {
        months = " " + res.getString(R.string.october);
    }
    else if(month == 11)
    {
        months = " " + res.getString(R.string.november);
    }
    else if(month == 12)
    {
        months = " " + res.getString(R.string.december);
    }

我知道这不是专业的,但它对我有用。

于 2012-05-15T10:48:36.867 回答
2

您需要修改原始响应,例如替换/Date/

String data = "/Date(1337032800000+0000)/";
String result = data.replaceAll("^/Date\\(" , "");
result = result.substring(0, result.indexOf('+'));

现在你有了1337032800000毫秒的日期。

于 2012-05-15T10:49:20.593 回答