Joda DateTime Json date format issue

798 views Asked by At

I would like the JSON output to be in a particular date format. My JSON output is:

"data":[[{"id":1,"createTimestamp":{"iMillis":1431463708237,"iChronology":{"iBase":{"iMinDaysInFirstWeek":4}}},"startTimestamp":{"iMillis":1420092000000,"iChronology":{"iBase":{"iMinDaysInFirstWeek":4}}},"endTimestamp":{"iMillis":1451541600000,"iChronology":{"iBase":{"iMinDaysInFirstWeek":4}}}}]]

But I would like the timestamps to be in this format:

2015-05-12T20:48:28.237Z

My code:

JSONArray a = new JSONArray();
a.add(aList);

JSONObject j = new JSONObject();
j.put("data", a);

JsonArray array = new JSONArray();
array.add(j);

String jason = gson.toJson(array);

I tried something like this, but not working

for (Noti n : aList) {
                DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
                dtf.parseDateTime(n.getCreateTimestamp().toString());
}

Thanks

2

There are 2 answers

1
Harry On BEST ANSWER

In the JS/Jquery, I used

var endTimestamp = new Date(data[i].endTimestamp.iMillis);  
2
gnasher729 On

Here's your time:

1431463708237

Note: No quotes. It's a number.

Here's your date format:

"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

Can you see how your time and your date format don't match at all?

Now look what the time is called: It's called "iMillis". Make a guess what this number could be measuring. Now take your favourite calculator and find out how many years it is. Now it's 2015, so make a guess when this time started counting. Then think about the significance of the year 1970, relating to time stamps. You might for example type "time stamp 1970" into Google.