My app is crashing when I run it. It worked the first time I built and compile it but now it doesn't. I think this is due to the "DateTimeFormatter" not being able to be used in my current api level 21. Is there a workaround this?
Here is the error:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/format/DateTimeFormatter;
Here is the code that is affected:
@RequiresApi(api = Build.VERSION_CODES.O)
public boolean checkTimeAgainstCurrentTime(String time) throws ParseException {
boolean isTime = false;
DateTimeFormatter parser = DateTimeFormatter.ofPattern("h:mma", Locale.ENGLISH);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime theTime = LocalTime.parse(time, parser);
Date time1 = new Date(System.currentTimeMillis());
Date time2 = new SimpleDateFormat("HH:mm:ss").parse(theTime + ":00");
time1.setHours(time2.getHours());
time1.setMinutes(time2.getMinutes());
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date today = Calendar.getInstance().getTime();
today.setHours(today.getHours()+8);
if (today.after(time1)) {
isTime = true;
}
// If true, means expired.
return isTime;
}
You can enable API Desugaring for backwards compatibility, here https://developer.android.com/studio/write/java8-support#library-desugaring are the instructions for it but essentially you will have to add one line of code, as shown below in the app level build.gradle
.
android {
compileOptions {
coreLibraryDesugaringEnabled true
}
}
and you need a dependency
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}