The main problem you are facing is that you are given time with a time zone format that is not supported by SimpleDateFormat .
There are two supported time zone formats that it can analyze,
Common time zones:
General time zone: Time zones are interpreted as text if they have names. For time zones representing a GMT offset value, the following syntax is used: GMTOffsetTimeZone: GMT Sign Hours : Minutes Sign: one of + - Hours: Digit Digit Digit Minutes: Digit Digit Digit: one of 0 1 2 3 4 5 6 7 8 9 Hours must be between 0 and 23, and Minutes must be between 00 and 59. The format is locale independent and digits must be taken from the Basic Latin block of the Unicode standard.
... and RFC 822:
RFC 822 time zone: For formatting, the RFC 822 4-digit time zone format is used: RFC822TimeZone: Sign TwoDigitHours Minutes TwoDigitHours: Digit Digit TwoDigitHours must be between 00 and 23. Other definitions are as for general time zones.
As you can see, the general time zone has a colon in it, but must have the prefix "GMT", while in RFC 822 there is no colon. What you are trying to make out is a kind of bastardization of the two.
Depending on the time zone format, one of the following will work: if you have legal syntax:
String generalInput = "2014-06-28T21:00:00GMT-05:00"; // legal General time zone String rfcInput = "2014-06-28T21:00:00-0500"; // legal RFC 822 time zone DateFormat generalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); // format for general time zone DateFormat rfcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // format for RFC 822 time zone
Since your input is incorrect, I would suggest that you are simply not trying to parse its part of the time zone and treat it like local time. Since you are still trying to convert 21:00 to 9:00 pm , this should work for you:
String input = "2014-06-28T21:00:00-05:00";
source share