Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate; error

I tried everything, but the error not getting solved

Here is my code:

public String[] getWeekDays() {

    LocalDate today = LocalDate.now();
    LocalDate monday = today;
    LocalDate tue = today;
    LocalDate wed = today;
    LocalDate thur = today;
    LocalDate fri = today;
    LocalDate sat = today;
    LocalDate sunday = today;
    while (sunday.getDayOfWeek() != DayOfWeek.SUNDAY){
        sunday = sunday.minusDays(1);
     }
     while (monday.getDayOfWeek() != DayOfWeek.MONDAY){
        monday = monday.minusDays(1);
     }
     while (tue.getDayOfWeek() != DayOfWeek.TUESDAY){
        tue = tue.plusDays(1);
     }
     while (wed.getDayOfWeek() != DayOfWeek.WEDNESDAY){
        wed = wed.plusDays(1);
     }
     while (thur.getDayOfWeek() != DayOfWeek.THURSDAY){
        thur = thur.plusDays(1);
     }
     while (fri.getDayOfWeek() != DayOfWeek.FRIDAY){
        fri = fri.plusDays(1);
     }
     while (sat.getDayOfWeek() != DayOfWeek.SATURDAY){
        sat = sat.plusDays(1);
     }

     String[] days = {String.valueOf(sunday),String.valueOf(monday), String.valueOf(tue), String.valueOf(wed),
                String.valueOf(thur), String.valueOf(fri), String.valueOf(sat)};
    return days;
}

I have written the code to get dates of one week. When I running this code, i am getting an error at LocalDate.now();. The error is "java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate;". I have searched on the net. Some are saying it requires API level min 27. I have tired on API 27 level but the error didn't get resolved. More ever my target device is API 26 I needed to run on this API level. And also tried adding "compile "java.time:LocalTime:1.8" ". This one also doesn't work. Please help me...

like image 499
Lets Explorer Avatar asked Dec 10 '22 05:12

Lets Explorer


1 Answers

NoClassDefFoundError-Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

LocalDate today = LocalDate.now(); // Added 1.8

Read LocalDate

Make sure, You added this in your build.gradle section.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    }

FYI

java.time package was added only in API 26.

So, You should set

 minSdkVersion 26

After that, Clean-Rebuild-Gradle.

like image 62
IntelliJ Amiya Avatar answered Feb 13 '23 03:02

IntelliJ Amiya