SimpleDateFormat timezone parsing?

If the format is that consistent, you could append 0:00 to the date string.

If the format is that consistent, you could append 0:00 to the date string. String dateString = "Sun, 04 Dec 2011 18:40:22 GMT+0"; SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss z", Locale. ENGLISH); Date date = sdf.

Parse(dateString + "0:00"); System.out. Println(date); (note that I fixed the SimpleDateFormat construction to explicitly specify the locale which would be used to parse the day of week and month names, otherwise it may fail on platforms which does not use English as default locale; I also wonder if you don't actually need HH instead of kk, but that aside).

One approach is to use normal string-manipulation techniques to translate your string from a form that you're expecting to a form that SimpleDateFormat will understand. You haven't said exactly what range of time-zone formats are acceptable, but one possibility is something like this: private static Date parse(String dateString) throws ParseException { final SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss Z"); dateString = dateString. ReplaceAll("(GMT+-)(\\d)$", "$1\\0$2"); dateString = dateString.

ReplaceAll("(GMT+-\\d\\d)$", "$1:00"); return dateFormat. Parse(dateString); } That would support GMT plus-or-minus a one-or-two-digit hour offset, in addition to still supporting anything already supported by SimpleDateFormat, such as EST or GMT+1030. Alternatively, if you know it will always be GMT, then you can just set the time-zone on the formatter, and ignore the time-zone in the string: private static Date parse(String dateString) throws ParseException { final SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss"); dateFormat.

SetTimeZone(TimeZone. GetTimeZone("GMT")); return dateFormat. Parse(dateString); } You can also split the difference.

I notice that the time-zone format in your string matches what's expected by TimeZone.getTimeZone(). Is that intentional? If so, you can grab that time-zone format out of the string, pass it to dateFormat.

SetTimeZone beforehand, and then ignore it during actual parsing: private static Date parse(final String dateString) throws ParseException { final SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss"); if(dateString. IndexOf("GMT") > 0) dateFormat. SetTimeZone ( TimeZone.

GetTimeZone (dateString. Substring(dateString. IndexOf("GMT"))) ); return dateFormat.

Parse(dateString); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions