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

Categories

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

datetime - How to convert "Fri Sep 21 15:23:59 CEST 2012" to "2012-09-21T15:23:59" in Java?

I want to convert the date string "Fri Sep 21 15:23:59 CEST 2012" to "2012-09-21T15:23:59" in Java. I tried this with SimpleDateFormat and the following code:

try {
    String dateString = "Fri Sep 21 15:23:59 CEST 2012";
    SimpleDateFormat input = new SimpleDateFormat("EEE MM dd HH:mm:ss z YYYY");
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    Date date = input.parse(dateString);
    System.out.println(output.format(date));            
} catch (ParseException e) {
    e.printStackTrace();
}

But the input parsing gives me a java.text.ParseException. I have read the documetation at http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html, but I am not able to find the error.

Which format string solves the input parsing of this string?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Use

"EEE MMM dd HH:mm:ss z YYYY"

Note 3 MMM


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...