I have a JSON object being returned to my app that contains a date
date": {
"year": 2011,
"month": 5,
"dayOfMonth": 30,
"hourOfDay": 16,
"minute": 13,
"second": 47
},
I need to use this date and store it in a SQL database, most likely as a string, so I parse this date object using the JSON functions in Java
Here I am trying to format the whole section as a string with dashes between the date and colons between the hours minutes seconds. Hoping that I could parse it with SimpleDateFormat, but that fails with parserexceptionerror
dataObj.getJSONObject("dateObject").getString("year") + "-" +
dataObj.getJSONObject("dateObject").getString("month") + "-" +
dataObj.getJSONObject("dateObject").getString("dayOfMonth") + " " +
dataObj.getJSONObject("dateObject").getString("hourOfDay") + ":" +
dataObj.getJSONObject("dateObject").getString("minute") + ":" +
dataObj.getJSONObject("dateObject").getString("second");
What would be the best way to go about this?
I need to take the JSON strings and combine them into a readable date which ideally I can turn into a string. This can be a formatted date – such as something simpledateformat will do, or it can be in milliseconds.
Insight Appreciated
Assuming that the goal is to transform a String representation of date information, formatted for example as “2011-5-30 16:13:47”, into a
java.util.Dateinstance…An alternative would be to turn all of the JSON input values into Java ints, and then use
Calendar.getInstance().set(year + 1900, month, date, hrs, min, sec).getTime().