简体   繁体   中英

Java, date from separate JSON values to string to date

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

If you have not already, you should take a look at JSON libraries that make json serialization and deserialization much easier. Jackson and Google's GSON are both excellent.

A JSON library knows how to read a date JSON string into a java.util.Date object. You can then store it in your database in a datetime column. The advantage of storing dates as proper datetime data types in databases is that you can build queries much easier- for example if you need to select all records between two dates. If that was a string column, your query will quickly get ugly. The other thing about datetime is that it's stored in a fixed length column (stores the milli second long values in many database implementations), so storage is efficient. Storing as a string can easily take more space if you start counting all the characters in a full SimpleDateFormat-ed string. As a result of that, building an index on a datetime becomes more efficient that building it on a varied-length string column.

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.Date instance...

String dateString = "2011-5-30 16:13:47";
Date date = new SimpleDateFormat("yyyy-M-dd HH:mm:ss").parse(dateString);
System.out.println(date);
// output: Mon May 30 16:13:47 MST 2011

String dateString2 = "2011-11-3 16:13:47";
Date date2 = new SimpleDateFormat("yyyy-M-dd HH:mm:ss").parse(dateString2);
System.out.println(date2);
// output: Thu Nov 03 16:13:47 MST 2011

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM