2

I have the latest AS 4.01 installed, and the latest dependency set up for Java 8 support with coreLibraryDesugaring (1.0.10), and proper compileOptions in my build.gradle file. The following code compiles and runs with no error on a device using API 23:

TemporalAccessor ta = null;
Instant i;
Date date = null;
try {
    int loc = 0;
    if ((loc = publishedDate.indexOf("+")) > 0) {
        // Offset time given
        publishedDate = adjustTimeFormat(publishedDate, loc);
        ta = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(publishedDate);
        i = Instant.from(ta);
    } else if ((loc = publishedDate.indexOf("-")) > 0) {
        publishedDate = adjustTimeFormat(publishedDate, loc);
        ta = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(publishedDate);
        i = Instant.from(ta);
    } else if (publishedDate.contains("Z")) {
        i = Instant.parse(publishedDate);
    } else {
        //
        ta = DateTimeFormatter.ISO_DATE.parse(publishedDate);
        i = Instant.from(ta);
    }
    date = Date.from(i);
} catch (Exception e) {
    ta = null;
}
if (ta == null) {
    date = new Date();
}
return date;

However, lint flags many of these lines as "requires API level 26 (current min is 21)"

I thought that with the desugaring in place, that it should allow this? Is this a lint bug?

tfrysinger
  • 1,306
  • 11
  • 26
  • As an aside I don’t believe the last `else` part will ever succeed. What you can parse using `ISO_DATE` will never be enough for defining an `Instant`. – Ole V.V. Sep 09 '20 at 05:11
  • That's interesting - you mean that even just the date aspect of the Instant won't be filled? – tfrysinger Sep 09 '20 at 23:40
  • Sorry, @tfrysinger, you have understood that correctly: your code will throw a `DateTimeException` (also an `Instant` hasn’t got any date aspect; internally it’s got a part for the seconds since the epoch and a part for the nanosecond of second). – Ole V.V. Sep 10 '20 at 03:25
  • I'm still confused - and perhaps I said it incorrectly - I don't mean "date part of instant" actually, but clearly the Date class knows how to use an Instant in order to create a Date, because the line date = Date.from(i) works fine in all the other cases. I just checked the docs on ISO_DATE and the way I'm using it as a 'catch-all' I don't see the issue: it says it will work with or without an offset, I just have it there in case the strings I'm processing don't fall into the other categories. What am I missing? – tfrysinger Sep 10 '20 at 14:11
  • I am not writing a lengthier explanation in comments, and since this is not what you asked, I am not writing it in an answer either. You may (1) try it out (2) trust me (3) ask a separate question about it and link to it from here, and I will take a look. – Ole V.V. Sep 10 '20 at 16:17
  • `ISO_DATE` won’t work as a catch-all. It will only catch the cases where the string is either in format uuuu-MM-dd or uuuu-MM-ddX (where X is a UTC offset). And maybe just adding to your confusion: an old-fashioned `Date` object does not represent a date. It represents a moment in time, just as `Instant` does (and at that moment it is not the same date in all time zones). – Ole V.V. Sep 10 '20 at 16:28

0 Answers0