Reputation: 2847
My JSON object looks like
{"iso":"2014-01-01T21:13:00.000Z","__type":"Date"}
I need to get the date from it as a date Object. What I have tried-
String dateStr = JsonObject.getString("iso"); //output is 2014-01-01T21:13:00.000Z
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date birthDate = sdf.parse(dateStr);
but this doesnt work, firstly it prompts me to add a try/catch which I do. When the debugger comes to Date birthDate = sdf.parse(dateStr); it just skips this.
How do I get a date out of my JSON object?
Upvotes: 1
Views: 8007
Reputation: 43758
Use a date format like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
to match the input string format.
For details, consult the documentation.
For example this
public static void main(String[] args) throws ParseException {
String dateStr = "2014-01-01T21:13:00.000Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Date birthDate = sdf.parse(dateStr);
System.out.println(birthDate);
}
prints (actual output depends on your time zone)
Wed Jan 01 22:13:00 CET 2014
Upvotes: 4