Search code examples
javaandroidweek-numberdate

Group list of Dates to week number? [Android]


I am working on an android app that will display a list of data. For example, if you start to use app today(28.05.2018)(MO) and I must calculate week number and add 7 days this week or you are starting Friday I must add 2 days this week.

I tried this method https://stackoverflow.com/a/42733001/9259044 but its wrong for me. First of all, I added dates and

 TreeMap<Integer, List<Date>> dateHashMap = new TreeMap<>();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    List<Date> spDates = new ArrayList<>();


    try {


        spDates.add(sdf.parse("02/06/2018"));
        spDates.add(sdf.parse("01/06/2018"));
        spDates.add(sdf.parse("31/05/2018"));
        spDates.add(sdf.parse("30/05/2018"));
        spDates.add(sdf.parse("29/05/2018"));

        spDates.add(sdf.parse("28/05/2018"));
        spDates.add(sdf.parse("27/05/2018"));

        spDates.add(sdf.parse("26/05/2018"));
        spDates.add(sdf.parse("25/05/2018"));


    } catch (ParseException e) {
        e.printStackTrace();
    }

I compare weekOfYear to my dates but this is wrong.

for (int i = 0; i < spDates.size(); i++) {
        List<Date> datesList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(spDates.get(i));

        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);


        for (Date date : spDates) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            if (weekOfYear == c.get(Calendar.WEEK_OF_YEAR)) {
                datesList.add(date);
            }
        }

        dateHashMap.put(weekOfYear, datesList);


    }

   Log.d("DATE",dateHashMap.toString());

Do you have any idea how can I group my Dates to week Number?


Solution

  • I think you want this:

        LocalDate today = LocalDate.now(ZoneId.of("Europe/Istanbul"));
        
        int weekNumber = today.get(WeekFields.ISO.weekOfYear());
        System.out.println("Week no. " + weekNumber);
        
        LocalDate[] days = today.datesUntil(today.with(TemporalAdjusters.next(DayOfWeek.MONDAY)))
                .toArray(LocalDate[]::new);
        System.out.println(Arrays.toString(days));
    

    Running it today (Monday, May 28) it printed

    Week no. 22
    [2018-05-28, 2018-05-29, 2018-05-30, 2018-05-31, 2018-06-01, 2018-06-02, 2018-06-03]
    

    It gives you all the dates from today, inclusive, until next Monday, exclusive. If Monday is the first day of the week, this means the remaining days of this week.

    I am using and recommending java.time, the modern Java date and time API. I find it much nicer to work with than the old date and time classes that you are using, Date, SimpleDateFormat and Calendar. java.time was introduced in Java 8 exactly because the old classes from Java 1.0 and 1.1 were too troublesome and error-prone.

    Question: Doesn’t java.time require Android API level 26?

    Edit: from the comment:

    Field requires API level 26

    java.time works nicely on both older and newer Android devices including lower API levels than 26.

    • In Java 8 and later and on newer Android devices (from API level 26 Oreo) the modern API comes built-in.
    • On older Android use desugaring, see the link below. Alternatively use the Android edition of ThreeTen Backport mentioned next. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.
    • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).

    Links