How do i parse the following date string in a valid java date? I am having trouble parsing the timezone.
"2013-10-10 10:43:44 GMT+5"
I am using the following method for parsing the date. It works well when the timezone is like "GMT+05:00" but fails to parse the above string even if i use different combinations of z, Z, X
public static Date convertStringWithTimezoneToDate(String dateString) {
if (dateString == null) {
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzzz");
Date convertedDate = null;
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;
}
Your date format is non-standard. The time zone must respect the syntax given in the documentation:
This code the transform your format into a standard one and construct a Java date object.
P.S.: Only one
z
is needed in the pattern string.