Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat timezone parsing

I'm having a tough time parsing this date its the +0 at the end that is causing a problem, does anyone know whats wrong with my format string?? If I remove the +0 from the date string and the Z from the format string it works fine, unfortunately for my application that isn't an option.

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SimpleDateFormat dateFormater = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss zZ");
        try {
            Date d = dateFormater.parse("Sun, 04 Dec 2011 18:40:22 GMT+0");
            System.out.println(d.toLocaleString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
like image 971
Nathan Schwermann Avatar asked Nov 25 '25 14:11

Nathan Schwermann


2 Answers

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);
}
like image 122
ruakh Avatar answered Nov 27 '25 04:11

ruakh


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)

like image 26
BalusC Avatar answered Nov 27 '25 04:11

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!