Parse GMT string to local
I've a date = 2011-04-17T22:02:00.001-07:00
that I'm parsing using SDF
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
formatter.parse(date)
I It 开发者_运维问答doesnt support -07:00 but -0700 works. What should change in the format?
It appears that SimpleDateFormat
doesn't support ISO8601 time zone formats. If you know for certain that your time zone will always end in the format of -##:##
(or +##:##
) then you could just remove the last :
so that your existing formatter works. E.g. this parses your date:
String input = "2011-04-17T22:02:00.001-07:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = sdf.parse(input.replaceAll(":(..)$", "$1"));
Be careful however, apparently ISO8601 allows for some variations whereby this wouldn't work.
take a look at :
Converting ISO 8601-compliant String to java.util.Date
it talks about this issue specifically..
精彩评论