Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to parse datetime string with SimpleDateFormat.parse() but I keep receiving Unparseable date exceptions.

Here is the date format I am trying to parse: 2011-10-06T12:00:00-08:00

Here is the code I am using:

try {
    String dateStr = "2011-10-06T12:00:00-08:00";
    SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MMM d, yyyy");
    Date date = dateParser.parse(dateStr);
    System.out.println(dateFormatter.format(date));         
} catch(Exception e) {
    System.out.println(e.getMessage());
}

Which returns this error: java.text.ParseException: Unparseable date: "2011-10-06T12:00:00-08:00"

As far as I know this is the correct way to use the SimpleDateFormat class but I'm not fluent in Java so I could be mistaken. Any one know what my issue is?

See Question&Answers more detail:os

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

1 Answer

The timezone should be GMT-08:00 or -0800 (as Madcore Tom said). See Java docs.

In Java 7 you can use "yyyy-MM-dd'T'HH:mm:ssX"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...