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?
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.
JavaScript Date toString() The toString() method returns a date object as a string.
stringify converts DateTimeOffset to UTC format #1710.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With