gpt4 book ai didi

java - 将日期对象从 json 转换为 android java

转载 作者:行者123 更新时间:2023-11-29 10:51:54 24 4
gpt4 key购买 nike

我有这种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"}]

我能够检索所有其他json objects除了news_date因为在我的Mysql table它的数据类型date这就是为什么它的行为如此,我怎样才能检索它 objectdate 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;
}
}

最佳答案

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

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

然后您可以使用 millis 来初始化日期对象

 Date date=new Date(millis);

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

关于java - 将日期对象从 json 转换为 android java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43584176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com