【问题标题】:Converting date object from json to android java [duplicate]将日期对象从json转换为android java [重复]
【发布时间】:2017-09-20 21:41:45
【问题描述】:

我有这种 json,它有这种奇怪格式的 news_date 对象

"news_date":"\/Date(1452412800000)\/"

下面是我的完整 json

[{"ID":1,"title":"Add News RSS and WordPress feeds into the ultimate Android News app","details":"If you ve ever wanted to create a specialized News app that takes WordPress feeds News feeds and ","photo_name":"hhh.jpg","news_date":"\/Date(1451721600000)\/","category":"News"},{"ID":2,"title":"Groupon","details":"Groupon is one of the best and most popular coupon apps available on Android","photo_name":"kkk.jpg","news_date":"\/Date(1452412800000)\/","category":"News"},{"ID":3,"title":"Kodi","details":"If you ve ever wanted to create a specialized News app that takes WordPress feeds News feeds and ","photo_name":"lll.jpg","news_date":"\/Date(1452672000000)\/","category":"News"},{"ID":48,"title":"Add News RSS and WordPress feeds into the ultimate Android News app","details":"If you ve ever wanted to create a specialized News app that takes WordPress feeds News feeds and ","photo_name":"nnn.jpg","news_date":"\/Date(1483430400000)\/","category":"News"},{"ID":49,"title":"Groupon","details":"Groupon is one of the best and most popular coupon apps available on Android","photo_name":"pp.jpg","news_date":"\/Date(1491721200000)\/","category":"News"},{"ID":50,"title":"Add News RSS and WordPress feeds into the ultimate Android News app","details":"If you ve ever wanted to create a specialized News app that takes WordPress feeds News feeds and ","photo_name":"sss.jpg","news_date":"\/Date(1484121600000)\/","category":"News"},{"ID":51,"title":"Groupon","details":"Groupon is one of the best and most popular coupon apps available on Android","photo_name":"vvv.jpg","news_date":"\/Date(1491721200000)\/","category":"News"}]

我能够检索除news_date 之外的所有其他json objects,因为在我的Mysql table 中,它的数据类型date,这就是它表现得那样的原因,我怎样才能检索到@987654330 @在date format.

下面是我的模型类

package model;

/**
 * Created by Admin on 1/28/2017.
 */

public class NewsObject {
    private String title;
    private String details;
    private String photo_name;
    private String news_date;


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public String getPhoto_name() {
        return photo_name;
    }

    public void setPhoto_name(String photo_name) {
        this.photo_name = photo_name;
    }

    public String getNews_date() {
        return news_date;
    }

    public void setNews_date(String news_date) {
        this.news_date = news_date;
    }
}

【问题讨论】:

  • 更改日期响应,然后使用simpledateformat格式化或解析日期
  • @AnandSavjani,请告诉我如何,假设我有一个字符串 String d = sr.getNews_Date(); 的初学者如何使用 SimpleDateFomat 更改该字符串名称
  • 从后端将 Date(1452412800000) 替换为 1452412800000
  • 参考这个将毫秒转换为时间stackoverflow.com/questions/17624335/…

标签: java android mysql json


【解决方案1】:

您收到的日期是 Date(Millis)。如果您可以控制它以从您接收的位置进行更改,那么推荐的方法是以“YYYY-MM-DDTHH:MM:SS+hh:ss”的 ISO 格式发送它。如果您无法更改它,那么您将需要像这样解析它。

 long millis = Long.valueOf(news_date.substring(news_date.indexOf("("), news_date.indexOf(")")));

然后你可以使用millis来初始化一个日期对象

 Date date=new Date(millis);

然后使用SimpleDateFormat 格式化日期并保存。

【讨论】:

猜你喜欢
  • 2011-10-25
  • 1970-01-01
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
相关资源
最近更新 更多