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
317 views
in Technique[技术] by (71.8m points)

java - Why can't this SimpleDateFormat parse this date string?

The SimpleDateFormat:

SimpleDateFormat pdf = new SimpleDateFormat("MM dd yyyy hh:mm:ss:SSSaa");

The exception thrown by pdf.parse("Mar 30 2010 5:27:40:140PM");:

java.text.ParseException: Unparseable date: "Mar 30 2010 5:27:40:140PM"

Any ideas?


Edit: thanks for the fast answers. You were all correct, I just missed that one key sentence in the SimpleDateFormat docs - I should probably call it a day.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

First, three-char months are to be represented by MMM. Second, one-two digit hours are to be represented by h. Third, Mar seems to be English, you'll need to supply a Locale.ENGLISH, else it won't work properly in machines with a different default locale.

The following works:

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy h:mm:ss:SSSa", Locale.ENGLISH);
System.out.println(sdf.parse("Mar 30 2010 5:27:40:140PM"));

Result (I'm at GMT-4 w/o DST):

Tue Mar 30 17:27:40 BOT 2010

Also see the java.text.SimpleDateFormat javadoc.

Why you called it pdf is beyond me, so I renamed it sdf ;)


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

1.4m articles

1.4m replys

5 comments

57.0k users

...