JSON parse date and time? - java

Im having a little issue with parsing json date.
Here is what I would like to parse:
{"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012 4:49:17 PM","suspended": "false","checkedin": "0"}
I am having trouble parsing "lastLatitudeUpdate" is it because there are spaces in between? Thanks in advance for the help.

Assuming you are on Android and therefore working with java (yes you don't mention that, only the tag in your question suggests it...)
Like mentioned here (and in various other places) you can parse a date in java using the SimpleDateFormat class:
SimpleDateFormat parserSDF=new SimpleDateFormat("M/d/yyyy h:m:s a");
Date d = parserSDF.parse(dateField,0);
Of course you have to first parse you json input with some library (e.g. standard library from json.org or Google gson) and then parse the string you'll get there for the field into a date.

Short answer: No, there is no way for the JSON engine to recognize a string as a Date object.
Long answer:
There is no 'date' type in JSON. However, this JSON is fine, the catch is that lastLatitudeUpdate will be parsed as a string. In order to convert this to a date you should try something like
var my_object= JSON.parse({"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012 4:49:17 PM","suspended": "false","checkedin": "0"});
my_object.lastLatitudeUpdate= Date.parse(my_object.lastLatitudeUpdate)
This function will give a timestamp. However, you have to make sure the string is correctly recognized, you may have to do some extra work.

Some links for hints
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
http://www.java-samples.com/showtutorial.php?tutorialid=406

How are you parsing the date? In Chrome this seems to work fine:
new Date("5/21/2012 4:49:17 PM");
Mon May 21 2012 16:49:17 GMT-0400 (US Eastern Daylight Time)

Related

Serialize Date: Swagger and Spring-MVC

I'm using swagger-ui and a stub server in Java Spring-MVC generated using swagger-codegen. I have a date format somewhere and if I enter some date, it always returns me the UNIX Date Time format. I would like to have the RFC-3339 format for the date and date-time. Does anyone knows how to do this?
I found the answer..
I had to overwrite the JsonFormat since I'm returning a JSON object..
Therefore, if you have the same problem as me, use:
#JsonFormat(pattern="...")
Date foo;
Do not forget to use import com.fasterxml.jackson.annotation.JsonFormat;

How to parse this date?

I want to parse below date in Java,
2012-11-29T09:15:00.002-08:00
Which date format I have to use to parse it?
java.text.SimpleDateFormat will parse it.
Actually, this format is an XSD date format, and the simplest way to parse it (without any use of an external library) is to use DataTypeConverter.parseDateTime(String lexicalXSDDateTime) in the javax.xml.bind package. This will return you a java.util.Calendar object, which you can retrieve a Date using Calendar.getTime().
Alternatively, there are solutions on SO that speaks about the same formatting, such as: What's the best way to parse an XML dateTime in Java?
enter link description here.
I hope this helps.
Using Date format as "yyyy-MM-dd'T'HH:mm:ss.SSSz"
E.g.:
String string = "2012-11-29T09:15:00.002-08:00";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssSSSz", Locale.ENGLISH).parse(string);
System.out.println(date);

Java parser and formatter for Atom DateFormat

Is there a date formatting tool for Atom Dates.
According to this link:
https://www.rfc-editor.org/rfc/rfc4287
Such date values happen to be compatible with the following
specifications: [ISO.8601.1988], [W3C.NOTE-datetime-19980827], and
[W3C.REC-xmlschema-2-20041028].
Example Date constructs:
<updated>2003-12-13T18:30:02Z</updated>
<updated>2003-12-13T18:30:02.25Z</updated>
<updated>2003-12-13T18:30:02+01:00</updated>
<updated>2003-12-13T18:30:02.25+01:00</updated>
I tried to use Joda ISODateTimeFormat.dateTime(); but it seems it doesn't handle the parsing when there is no milliseconds (2003-12-13T18:30:02Z for exemple).
What is the simplest way to parse all these date formats?
This is ISO 8601 format, the standard format used in for example XML. Joda Time supports this format very well, you can just pass these strings to the constructor of DateTime:
DateTime timestamp = new DateTime("2003-12-13T18:30:02Z");
Works without any problems, also if there are no milliseconds in the string.
It seems to be xml dateTime. Then the best choice is javax.xml.datatype.XMLGregorianCalendar.
DatatypeFactory f = DatatypeFactory.newInstance();
XMLGregorianCalendar xgc = f.newXMLGregorianCalendar("2003-12-13T18:30:02.25Z");
System.out.println(xgc);
System.out.println(xgc.toGregorianCalendar().getTime());
output
2003-12-13T18:30:02.25Z
Sat Dec 13 20:30:02 EET 2003
See more in API
DateUtils from Apache Commons / Lang has a parseDate method that supports multiple patterns. That may work for you. (The patterns must be formatted according to the SimpleDateFormat syntax)

Unparseable date with SimpleXml

I am using Simple XML for XML serialization in my Android project. I have problem with parsing a Date object. I receive an exception:
Unparseable date: 2012-05-01T08:22:34+02:00
Can anyone help me how to tell Simple XML what the date format is? Thanks.
SimpleXML only supports some DateFormat's, but you can use a custom Transform for Dates.
Try my example i posted here: Parse date with SimpleFramework
You have a timezone at the end of your date. Java can parse timezone offsets, but without the ยด:' divider between. So if your date timezone were +0200 instead of +02:00, it should work. You could run it through a SimpleDateFormatter.

Parse org.mozilla.javascript.NativeDate in Java.util.Date

I'm trying to parse a date that i get from JavaScript script evaluated with rhino library into java.util.Date, can i convert a org.mozilla.javascript.NativeDate into a java.util.Date ?
If convert NativeDate into a string with the Context.tostring method i get a date in the following format :
Wed Oct 12 2011 16:17:59 GMT+0200 (CEST)
How can i parse this string date representation in to a java.util.Date object ?
In Rhino use
context.jsToJava(nativeDateObj, Date.class);
Bvesco's answer works well. However doing this the other way round (java to js) is not entirely as simple - Context.javaTojs() does not work for dates. I eventually found the solution here - use the javascript constructor:
Object js = context.newObject(scope, "Date", new Object[] {date.getTime()});
The above post also mentioned the following alternative to convert a date from js to java (I haven't confirmed this):
Date date = new Date((long) ScriptRuntime.toNumber(s));
Have you tried;?
java.sql.Date.valueOf("date string");

Categories