0

I been stuck trying to parse the following date format below:

2016-04-27T00:00:00+10:00

I've tried many combinations and can't seem to get it to work.

I would have thought this would work as it make the most sense to me - yyyy-MM-dd'T'HH:mmZ

Any ideas? Thanks

Petersicecream
  • 341
  • 4
  • 11

2 Answers2

2

You are unable to parse it as you are not using the correct format. It should be yyyy-MM-dd'T'HH:mm:ssZ instead of yyyy-MM-dd'T'HH:mmZ.

Here is the code snippet:

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String input = "2016-04-27T00:00:00+10:00";
Date date = sf.parse(input);

Output:

Tue Apr 26 14:00:00 GMT 2016
user2004685
  • 9,548
  • 5
  • 37
  • 54
  • This is the correct answer, Thanks. I was adding the single quotations around the Z which was wrong as well. – Petersicecream Apr 21 '16 at 09:28
  • @Petersicecream If you are doing something like `'Z'` then it looks for the literal `Z` and since you are not getting the literal but the TimeZone it fails. – user2004685 Apr 21 '16 at 09:53
1

In case you use Jackson for JSON parsing, you can simply annotate date field with @JsonFormat Annotation. Here's an example:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss'Z'", timezone="GMT")
public Date date;
localhost
  • 5,568
  • 1
  • 33
  • 53