Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android API-21中DateTimeFormatter的替代方案_Java_Android_Android Studio_Android Api Levels - Fatal编程技术网

Java Android API-21中DateTimeFormatter的替代方案

Java Android API-21中DateTimeFormatter的替代方案,java,android,android-studio,android-api-levels,Java,Android,Android Studio,Android Api Levels,我的应用程序在运行时崩溃。我第一次构建和编译它时,它就工作了,但现在不行了。我认为这是因为“DateTimeFormatter”不能在我当前的api级别21中使用。有解决办法吗 以下是错误: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/format/DateTimeFormatter; 以下是受影响的代码: @RequiresApi(api = Build.VERSION_CODES.O) public bo

我的应用程序在运行时崩溃。我第一次构建和编译它时,它就工作了,但现在不行了。我认为这是因为“DateTimeFormatter”不能在我当前的api级别21中使用。有解决办法吗

以下是错误:

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/format/DateTimeFormatter;
以下是受影响的代码:

@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;
}

您可以启用API Desugaring以实现向后兼容性,以下是相关说明,但实际上您必须添加一行代码,如下面的应用程序级别
build.gradle
中所示

android {
  compileOptions {
    coreLibraryDesugaringEnabled true
  }
}
你需要依赖

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}

您可以启用API Desugaring以实现向后兼容性,以下是相关说明,但实际上您必须添加一行代码,如下面的应用程序级别
build.gradle
中所示

android {
  compileOptions {
    coreLibraryDesugaringEnabled true
  }
}
你需要依赖

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}