0

Am having this kind of json,it has news_date object in this weird format

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

below is my full 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"}]

am able to retrieve all other json objects except news_date because in my Mysql table it's data Type is date that's why it's behaving like that, how can i retrieve that object in date format.

below is my model class

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;
    }
}
Fauzia Nito
  • 75
  • 1
  • 2
  • 7
  • change response of date and then use simpledateformat to format or parse date – Anand Savjani Apr 24 '17 at 09:20
  • @AnandSavjani,plz show me how to , am a beginner suppose ma having a String `String d = sr.getNews_Date();` how can i change that String Name with SimpleDateFomat – Fauzia Nito Apr 24 '17 at 09:28
  • replace `Date(1452412800000)` to `1452412800000` from backend side – Anand Savjani Apr 24 '17 at 09:31
  • refer this to convert millisecond to time http://stackoverflow.com/questions/17624335/converting-milliseconds-to-minutes-and-seconds – Anand Savjani Apr 24 '17 at 09:31
  • refer this link http://stackoverflow.com/questions/36902369/how-to-deserialise-net-jsonresult-datetime-format-with-gson-in-android/36903948#36903948 – Krish Apr 24 '17 at 10:12

1 Answers1

1

The date you are receiving is in Date(Millis). If you have control over it to be changed from where you are receiving then recommended way would be to send it in the ISO format which is 'YYYY-MM-DDTHH:MM:SS+hh:ss'. If you cannot change it then you will need to parse it out like this.

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

you can then use millis to initialise a date object

 Date date=new Date(millis);

and then use SimpleDateFormat to format the Date and save it.

harshitpthk
  • 4,058
  • 2
  • 24
  • 32