安卓:解析JSON日期

问题描述:

我是从服务器获取一个JSON对象,这种格式"Date": "/Date(1337032800000+0000)/",我需要它像“4月1日”。安卓:解析JSON日期

我试图解析它这样,但我总是得到一个"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(); 
        } 

非常感谢提前!

您需要修改原始响应像更换/日期/

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

现在你有1337032800000这是迄今为止在毫秒。

+0

这个名字,它只是在开头修饰部分,而不是结尾 – noloman

这是时间戳吗?为什么你就是不改串地龙和:

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

我知道这是不是亲的,但它为我工作。

+0

这个工作,但我怎样才能定制的日期格式显示?我只需要“4月1日” – noloman

+0

尝试:date.toLocaleString() – goodm

+0

好转,但仍时例如,我不需要=) – noloman