如何将JSON对象日期解析为Android?

3

如何在Android中解析类似于\/Date(1391802454790-0700)\/的json日期格式,转换为(01/31/2014 11:44 AM)

API结果:

{
    "Comment":"ogogog",
    "CommentDate":"\/Date(1391802454790-0700)\/",
    "CommentReply":[
    ],
    "NumberOfLikes":1,   
    "UniqueId":"f786e0da-2e4a-430f-a37c-c60db1901e38",
    "VideoId":65   
}

我尝试了下面的代码,但是没有显示出这种格式 (01/31/2014 11:44 AM)。
String json = "\/Date(1391802454790-0700)\/"; 
//String json = "commentReplyCommentDate";
json=json.replace("/Date(", "").replace("-0700)/", "");
long time = Long.parseLong(json);
Date d= new Date(time);
System.out.println("Dateeee---->"+d);

1
我会假设日期前面的部分是自纪元以来的毫秒数,破折号后面是相对于GMT的时区偏移量。将其放入日历对象中,你就可以了。 - Gabe Sechan
1个回答

0

你应该使用 MM/dd/yyyy hh:mm a 格式化 d 日期。

    String json = "/Date(1391802454790-0700)/";
    json=json.replace("/Date(", "").replace("-0700)/", "");
    long time = Long.parseLong(json);
    Date d= new Date(time);
    System.out.println("Dateeee---->"+d);
    System.out.println(new SimpleDateFormat("MM/dd/yyyy hh:mm a").format(d));

输出:

   Dateeee---->Sat Feb 08 01:17:34 IST 2014
   02/08/2014 01:17 AM

谢谢,我得到了完美的结果。 - KeTaN
@RuchiraGayanRanaweera 对我不起作用...错误提示无效的长整型 - Vaibs_Cool

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接