Search code examples
androiddateandroid-calendar

Android ThreeTenAbp get all weeks of the year to array


So I'm trying to load all weeks of the year using ThreeTenABP library, but I have no idea where to start because there is no documentation. At least I couldn't find any. Please help me out.


Solution

  • ThreeTenABP is just an Android-specific adaptation of the ThreeTen-Backport Java project.

    ThreeTen-Backport is a back-port of the Java SE 8 date-time classes (java.time) to Java SE 6 and 7. You can find its documentation here.

    Code from this answer:

    private static long getNumberOfWeeksInYear(LocalDate date) {
        LocalDate middleOfYear = date.withDayOfMonth(1).withMonth(6);
        return middleOfYear.range(WeekFields.ISO.weekOfWeekBasedYear()).getMaximum();
    }
    

    And you can call it passing the following parameter:

    LocalDate.of(year, 1, 1) // replace year by the the year you want, e.g. 2009