Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Date from a JSON object

I have a JSON object, which have one field that is a birthdate:

JSONObject obj = new JSONObject(response); User user = new User(); user.setuserID(obj.getString("userID")); user.setisMale(obj.getBoolean("isMale")); user.setEmail(obj.getString("email")); // user.setBirthdate(obj.getDate("birthdate")); user.setLastName(obj.getString("lastName")); user.setFirstName(obj.getString("firstName")); 

But the method getDate() does not exist in JSONObject.
How can I set the Birthdate in my User object?

like image 521
user2144555 Avatar asked Apr 02 '13 09:04

user2144555


People also ask

Can JSON represent dates?

JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string in ISO 8601 format.

How do I Stringify a date?

JavaScript Date toString() The toString() method returns a date object as a string.

Does JSON Stringify convert date to UTC?

stringify converts DateTimeOffset to UTC format #1710.


1 Answers

You may do like below,

String dateStr = obj.getString("birthdate"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date birthDate = sdf.parse(dateStr); //then user.setBirthdate(birthDate); 

Hope to help you :)

like image 152
Hunter Zhao Avatar answered Sep 28 '22 02:09

Hunter Zhao