-1

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?

parthi
  • 424
  • 1
  • 4
  • 16
TsigumEnes
  • 131
  • 1
  • 14
  • What you describe as your attempted solution doesn't sound like it would solve the problem you've stated. Help me understand. Are you saying that you don't know how to get the delta between the given date and Sunday? Or are you having trouble taking a day in a week and resolving that to some number of weeks since some date? In which case, all you should have to do is calculate the delta and divide by 7? – jake.toString May 28 '18 at 11:02
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. May 28 '18 at 11:04
  • @OleV.V. I know but all month finished 30 or 31.I dont want to finished 30 or 31 to calculate the week number.For example Week 22 (28.06.2018-03.06.2018) like this. – TsigumEnes May 28 '18 at 11:07
  • Can you use the Java stream API with your API level? It sounds like its grouping capabilities are just right for you. – Ole V.V. May 28 '18 at 11:10
  • Could you please explain what’s wrong with the results you get from the code you have taken from that linked answer? Asking because to me it would seem that the output quoted there would fit what I thought you wanted. – Ole V.V. May 28 '18 at 11:15
  • 1
    Upsy sorry man my mistake I check my output thats correct.Sorry this code works fine – TsigumEnes May 28 '18 at 11:32

1 Answers1

0

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161