1

I have a DateTime in yyyy-mm-ddThh:mm:ss.nnnnnn+|-hh:mm which I want to convert in this format MMMM-yyyy I have successfully done this using the java.time API, but have not been able to do this with SimpleDateTimeFormatter. Because java.time is only supported android O onwards, I still need to support older devices. My current code which fails and gives an Exception is

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                ZonedDateTime datetime = ZonedDateTime.parse(argDate);
                DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MMMM-yyyy");
                String formatDateTime = datetime.format(targetFormatter);
                return formatDateTime;
    } else{ 
        try {
        DateFormat originalFormat = new SimpleDateFormat("yyyy-mm-ddThh:mm:ss.nnnnnn+|-hh:mm", Locale.ENGLISH);
        DateFormat targetFormat = new SimpleDateFormat("MMMM-yyyy");
        Date date = originalFormat.parse(argDate);
        String formattedDate = targetFormat.format(date);  // 20120821
        return formattedDate;
    }catch(Exception ex){
        ex.printStackTrace();
    }
    }

Exception is

W/System.err: java.lang.IllegalArgumentException: Illegal pattern character 'T' 2019-08-07 21:53:24.445 14123-14123/com.chat W/System.err: at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:873) 2019-08-07 21:53:24.445 14123-14123/com.chat W/System.err:
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:687) 2019-08-07 21:53:24.445 14123-14123/com.chat W/System.err:
at java.text.SimpleDateFormat.(SimpleDateFormat.java:658) 2019-08-07 21:53:24.445 14123-14123/com.chat W/System.err:
at .ui.ContactDetailsActivity.formatDate(ContactDetailsActivity.java:652) 2019-08-07 21:53:24.445 14123-14123/com.chat W/System.err:
at .ui.ContactDetailsActivity.fillUserCompanyDetails(ContactDetailsActivity.java:620) 2019-08-07 21:53:24.445 14123-14123/com.chat W/System.err:
at .ui.ContactDetailsActivity.showUIFromData(ContactDetailsActivity.java:562) 2019-08-07 21:53:24.445 14123-14123/com..chat W/System.err:
at .ui.ContactDetailsActivity.access$300(ContactDetailsActivity.java:77) 2019-08-07 21:53:24.445 14123-14123/com.chat W/System.err:
at .ui.ContactDetailsActivity$6.onResponse(ContactDetailsActivity.java:544) 2019-08-07 21:53:24.446 14123-14123/com.chat W/System.err:
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) 2019-08-07 21:53:24.446 14123-14123/com.chat W/System.err:
at android.os.Handler.handleCallback(Handler.java:873) 2019-08-07 21:53:24.446 14123-14123/com.chat W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99) 2019-08-07 21:53:24.446 14123-14123/com.chat W/System.err: at android.os.Looper.loop(Looper.java:193) 2019-08-07 21:53:24.446 14123-14123/com.chat W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6718) 2019-08-07 21:53:24.446 14123-14123/com.chat W/System.err: at java.lang.reflect.Method.invoke(Native Method) 2019-08-07 21:53:24.446 14123-14123/com.chat W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 2019-08-07 21:53:24.446 14123-14123/com.chat W/System.err:
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Can someone help me with this. I think I got the format incorrect for the time 2019-08-06T11:52:49:416472+5:30 I have taken the format E8601DZw.d which is described as yyyy-mm-ddThh:mm:ss.nnnnnn+|-hh:mm here

ichthyocentaurs
  • 2,173
  • 21
  • 35
  • 1
    Possible duplicate of [Illegal pattern character 'T' when parsing a date string to java.util.Date](https://stackoverflow.com/questions/2597083/illegal-pattern-character-t-when-parsing-a-date-string-to-java-util-date) – Andrii Omelchenko Aug 07 '19 at 16:55
  • Take a look at [this](https://stackoverflow.com/q/2597083/6950238) question and answers. – Andrii Omelchenko Aug 07 '19 at 16:56
  • 1
    @AndriiOmelchenko While that question is about one part of the problem in this question, it cannot suffice for solving it entirely. This question is only partially a dupe of that one. – Ole V.V. Aug 07 '19 at 17:10

1 Answers1

1

ThreeTenABP

Since you’ve got a working solution using java.time and since SimpleDateFormat is causing trouble (not only for you: it is notoriously troublesome), I suggest that you use the backport of java.time. It has been adapted to Android in the ThreeTenABP project. See the links at the bottom.

Strictly speaking only the central and most used parts of java.time have been backported. However, that covers all that is used in at least 99 % of programs that use java.time, and I am sure that the solution that you have will work unchanged on the backport too. When I say unchanged, one change is necessary: you must import the date and time classes from org.threeten.bp with subpackages.

In case you do insist on doing without the backport I also include links to questions about parsing ISO 8601 format. ISO 8601 is an international standard and this is the format you have got. Be aware, however: There is no way whatsoever that SimpleDateFormat can correctly parse 6 decimals on the seconds. It supports only milliseconds, exactly three decimals (not 2, not 4, not 6).

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    “yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX” date format. This one is actually it. I fixed this one a couple of hours ago. But this is the format I ought to have been using – ichthyocentaurs Aug 07 '19 at 19:23