-4

I am receiving response of Date in JSON format like /Date(1521866513877+0530)/ which I want in format like this 24/03/2018 10:11:53.

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
  • 1
    There is no such thing as a "JSON Date Format". It is a format defined by the system producing this JSON. – Henry Mar 24 '18 at 05:25
  • 1
    Ok feel free to convert it. – Arun Sudhakaran Mar 24 '18 at 05:34
  • Well, once you extract the various elements of the text, you could use something like `LocalDateTime ld = LocalDateTime.ofEpochSecond(1521866513877L / 1000, 0, ZoneOffset.ofHoursMinutes(5, 30));` which will give you a `LocalDateTime` value you can then format – MadProgrammer Mar 24 '18 at 06:02
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 24 '18 at 07:27
  • What did your search and research effort bring up, and in what way was it insufficient? Please see more [here: I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/) and [here: How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Ole V.V. Mar 25 '18 at 09:39

3 Answers3

0

If there's no native way to parse that, try this regex pattern:

\/Date\((\d+)\+(\d+)\)\/

I'm not a java guy but regex is everywhere. I looks like the date is in miliseconds so you need to divide 1521866513877 / 1000 to get the timestamp. Here's a sample in PHP:

preg_match_all('/\/Date\((\d+)\+(\d+)\)\//i', '/Date(1521866513877+0530)/', $matches);
if ($matches[0]) {
    $timestamp = $matches[1][0];
    $offset = $matches[2][0];

    echo date('m/d/Y H:i:s', (int)$timestamp / 1000);
}
lodev09
  • 302
  • 1
  • 9
0

The Gson class may help you to serialize format like that. And default it use a SimpleDateFormat object to serialize. Use GsonBuilder to get a new Gson object to serialize date format like 24/03/2018 10:11:53:

Gson gson = new GsonBuilder()  
  .setDateFormat("dd/MM/yyyy HH:mm:ss")  
  .create();  
Jswq
  • 710
  • 1
  • 4
  • 22
0

Use a regular expression to extract the individual pieces from the string. The 13 digit number is a count of milliseconds since the epoch. Parse it as a long and convert it to an Instant (class from java.time, the modern Java date and time API). Parse +0530 into a ZoneOffset; it’s an offset of 5 hours 30 minutes ahead of UTC or GMT. Combine the Instant and the ZoneOffset into an OffsetDateTime. Format it using a DateTimeFormatter. That’s all.

If there are some of the pieces you still don’t know how to do, use your search engine. All of this is explained in several places on the Internet. If after reading the tutorials and the API documentation and trying your best you still get stuck, ask a much more specific question here on Stack Overflow. Remember to include a report of your research and your best attempt and how it fails. We will be able to help you much better from there.

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117