Problem converting DateFormat to java?

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

The output template is similar:

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

i will transform it as follows:

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

The output should be like this: Thu. March 24. Idea 2010

but I get an illegalArgumentException . Do not know why? any idea?

Stacktrace post:

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)
+3
source share
1 answer

The problem in this part:

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

Apparently, it does not accept dates / times in this format.

You should not use this constructor in any case - create an appropriate formatter to parse this particular format, and then parse it with that.

Joda Time DateFormat . , Joda Time Android, ... .

: :

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
+13

Source: https://habr.com/ru/post/1740322/


All Articles