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

4

10

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

Yet answered 26/9, 2018 at 5:14 Comment(0)
P
18

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.

Portraitist answered 26/9, 2018 at 5:18 Comment(0)
M
4

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:

    LocalDate today = LocalDate.now(ZoneId.of("America/Tortola"));
    LocalDate monday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
    LocalDate tue = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
    LocalDate wed = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));
    LocalDate thur = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY));
    LocalDate fri = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
    LocalDate sat = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
    LocalDate sunday = 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

Matty answered 26/9, 2018 at 13:55 Comment(0)
S
4

Use Java 8 API desugaring officially supported by Google: https://developer.android.com/studio/write/java8-support

It enables you to use many Java 8 APIs on older platforms.

Syrup answered 20/4, 2022 at 9:38 Comment(0)
A
2

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

    private String getCurrentDateTime() {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            Log.d(TAG, "getCurrentDateTime: greater than O");
            return LocalDateTime.now().format(DateTimeFormatter.ofPattern("h:m a"));
        } else{
            Log.d(TAG, "getCurrentDateTime: less than O");
            SimpleDateFormat SDFormat = new SimpleDateFormat("h:m a");
            return SDFormat.format(new Date());
        }
}
Antiperistalsis answered 28/4, 2021 at 13:17 Comment(1)
I handled it this way, Good answer, keeping backward compatibility in viewDevise

© 2022 - 2024 — McMap. All rights reserved.