I'm receiving a JSON object with date value like this:
{"PostingDate":"\/Date(1325134800000-0500)\/"}
And I want to parse it in Java code to Date
or getting it as a String
.
I want to know what is the easy way of doing it.
I'm receiving a JSON object with date value like this:
{"PostingDate":"\/Date(1325134800000-0500)\/"}
And I want to parse it in Java code to Date
or getting it as a String
.
I want to know what is the easy way of doing it.
I have created a simple JavaScript function using Jquery DatePicker
$('#Started').val($.datepicker.formatDate('mm/dd/yy', JsonToJSDate(yourDateVarHere)));
Simple thing but handling my job. Extract your object value from JSON and apply substring.
e.g:
now parse millis and use them where ever you want.
I take it the first number (
1325134800000
) is the number of milliseconds since epoch, and-0500
is the time zone. This appears to be the case given the sample code below, which seems to do what you want.The following code parses the JSON input using Jackson, which I recommend if you don't have a JSON parsing library of choice yet. It lacks error checking etc.
Sample code:
Output:
In Java >= 8 you can use the new
java.time
API.The input contains:
1325134800000
), which is the number of milliseconds since unix epoch (1970-01-01T00:00Z
)-0500
), which is the difference from UTC (in this case, 5 hours behind UTC)In the new
java.time
API, there are lots of different types of date/time objects. In this case, we can choose to use ajava.time.Instant
(which represent a count of nanoseconds since unix epoch) or ajava.time.OffsetDateTime
(which represents theInstant
converted to a date/time in a specific offset).To parse the
String
, I use ajava.time.format.DateTimeFormatterBuilder
to create ajava.time.format.DateTimeFormatter
. I also use ajava.time.temporal.ChronoField
to specify which fields I'm parsing:I also use a regex to extract just the relevant part from the input
String
(although you could also usesubstring()
to get it):Then I can parse to the type I want:
The
Instant
is equivalent to2011-12-29T05:00:00Z
(Instant
is just a point in the timeline, and you can think that's always in UTC). TheOffsetDateTime
has the same instant, but converted to-0500
offset, so its value is2011-12-29T00:00-05:00
. But bothInstant
andOffsetDateTime
represents the same point in time.To convert to
java.util.Date
, use theInstant
:That's because the
java.util.Date
has no timezone/offset information and it just represents the count of milliseconds since unix epoch (the same concept ofInstant
), so it can be easily converted from anInstant
.Java 6 and 7
For Java 6 and 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it here).
The difference from Java 8 is the package names (in Java 8 is
java.time
and in ThreeTen Backport (or Android's ThreeTenABP) isorg.threeten.bp
), but the classes and methods names are the same. So the formatter creation and the parsing code toInstant
andOffsetDateTime
are the same.Another difference is that, in Java <= 7, the
java.util.Date
class has nofrom()
method. But you can use theorg.threeten.bp.DateTimeUtils
class to do the conversion:Hier is a working parsing method based on the version of fge but improved as
=>