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

后端 未结 3 855
情话喂你
情话喂你 2021-01-20 03:20

I tried everything, but the error not getting solved

Here is my code:

public String[] getWeekDays() {

    LocalDate today = LocalDate.now();
    Loc         


        
3条回答
  •  梦毁少年i
    2021-01-20 04:22

    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

    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    • Comment about dependencies by Satyajit Tarafdar

提交回复
热议问题