Skip to content Skip to sidebar Skip to footer

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; Loca

Solution 1:

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.

LocalDatetoday= 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.

Solution 2:

You are trying to run your app on a device (or simulator) with an API level below 26. LocalDate and the rest of java.time were introduced in level 26. However, there is a backport that allows you to use the most used 80 % of it on earlier Android versions. I suggest that this is a good solution for you.

So add the ThreeTenABP library to your project. And make sure you import org.threeten.bp.LocalDate and org.threeten.bp.DayOfWeek rather than the versions from java.time. Then you should be fine.

I was once told that dependencies is (I didn’t test):

compile group: 'org.threeten', name: 'threetenbp', version: '1.3.3', classifier: 'no-tzdb'

TemporalAdjusters

As an aside your code may be written as:

LocalDatetoday= LocalDate.now(ZoneId.of("America/Tortola"));
    LocalDatemonday= today.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
    LocalDatetue= today.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
    LocalDatewed= today.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));
    LocalDatethur= today.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY));
    LocalDatefri= today.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
    LocalDatesat= today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
    LocalDatesunday= today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

I have used the following imports:

import org.threeten.bp.DayOfWeek;
import org.threeten.bp.LocalDate;
import org.threeten.bp.ZoneId;
import org.threeten.bp.temporal.TemporalAdjusters;

Links

Solution 3:

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. The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

Solution 4:

Use SimpleDateFormat and Calendar. You may also need different implementations for pre and post sdk 26

privateStringgetCurrentDateTime() {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            Log.d(TAG, "getCurrentDateTime: greater than O");
            returnLocalDateTime.now().format(DateTimeFormatter.ofPattern("h:m a"));
        } else{
            Log.d(TAG, "getCurrentDateTime: less than O");
            SimpleDateFormatSDFormat = newSimpleDateFormat("h:m a");
            returnSDFormat.format(newDate());
        }
}

Post a Comment for "Java.lang.noclassdeffounderror: Failed Resolution Of: Ljava/time/localdate; Error"