Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

java - Parsing a String with a GMT timezone to Date using SimpleDateFormat

I'm having problem parsing the Date from an input string that is of the following format:

String input = "Fri Jul 15 12:00:00 GMT+300 2011";
String dateFormat  = "EEE MMM d HH:mm:ss z yyyy";
Date date = new SimpleDateFormat(dateFormat).parse(input);

An exception is thrown:

java.text.ParseException: Unparseable date: "Fri Jul 15 12:00:00 GMT+300 2011"
    at java.text.DateFormat.parse(DateFormat.java:337)

I bet it has got something to do with the GMT string. I think I've tried it with z, zzz, zZ, and zzzZ. Any thoughts? Is the input GMT+300 even a standard, valid input format?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem was that GMT+300 isn't valid GMT string according to the Java Timezone specification.

Solution: Manipulating the timezone portion of input string. GMT+300 ==> GMT+3:00


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...