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

android - DateFormat conversion problem in java?

my input String is : 2010-03-24T17:28:50.000Z

output pattern is like:

DateFormat formatter1 = new SimpleDateFormat("EEE. MMM. d. yyyy");

i convert this like this:

formatter1.format(new Date("2010-03-24T17:28:50.000Z"));//illegalArgumentException here the string "2010-03-24T17:28:50.000Z"

ouput should be like this: Thu. Mar. 24. 2010 idea

but i get a illegalArgumentException. Dont know why? any idea??

stacktrace message is:

04-08 19:50:28.326: WARN/System.err(306): java.lang.IllegalArgumentException
04-08 19:50:28.345: WARN/System.err(306):     at java.util.Date.parse(Date.java:447)
04-08 19:50:28.355: WARN/System.err(306):     at java.util.Date.<init>(Date.java:157)
04-08 19:50:28.366: WARN/System.err(306):     at com.example.brown.Bru_Tube$SelectDataTask.doInBackground(Bru_Tube.java:222)
04-08 19:50:28.366: WARN/System.err(306):     at com.example.brown.Bru_Tube$SelectDataTask.doInBackground(Bru_Tube.java:1)
04-08 19:50:28.405: WARN/System.err(306):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-08 19:50:28.415: WARN/System.err(306):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-08 19:50:28.415: WARN/System.err(306):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-08 19:50:28.446: WARN/System.err(306):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-08 19:50:28.456: WARN/System.err(306):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-08 19:50:28.466: WARN/System.err(306):     at java.lang.Thread.run(Thread.java:1096)
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 is in this part:

new Date("2010-03-24T17:28:50.000Z")

Apparently it doesn't accept dates/times in that format.

You shouldn't be using that constructor anyway - create an appropriate formatter to parse that particular format, and then parse it with that.

Alternatively, use Joda Time to start with, and avoid using DateFormat completely. I don't know if you can use Joda Time from Android, mind you... and it's fairly large.

EDIT: To spell it out explicitly:

String inputText = "2010-03-24T17:28:50.000Z";
// "Z" appears not to be supported for some reason.
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat outputFormat = new SimpleDateFormat("EEE. MMM. d. yyyy");
Date parsed = inputFormat.parse(inputText);
String outputText = outputFormat.format(parsed);

// Output is Wed. Mar. 24 2010 on my box

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

...