ahmad
ahmad

Reputation: 89

How to get all previous weeks

How to get all weeks of month with date,I am doing like this

  calendar.add(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() - calendar.get(Calendar.DAY_OF_WEEK));
    String[] weekly = new String[7];
    Arrays.fill(weekly, "");
    int today = calendar.getInstance().get(Calendar.DAY_OF_WEEK);
    for (int i = 0; i < 7; i++) {
        Date dt = calendar.getTime ();
        // now format it using SimpleDateFormat
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

        String val = df.format (dt);
        weekly[i] = val;
        calendar.add (Calendar.DAY_OF_WEEK, 2);
        Log.d("valueweek",""+weekly[i]);

    }

output

26-11-2017

27-11-2017

28-11-2017

29-11-2017

30-11-2017

01-12-2017

02-12-2017

But i also want all previous weeks of this month

Upvotes: 1

Views: 58

Answers (1)

Anonymous
Anonymous

Reputation: 86296

java.time

First, do consider to drop the long outmoded classes Calendar, Date and DateFormat. Today we have so much better in java.time, the modern Java date and time API also known as JSR-310. On Android too, I will return to that. The modern API is so much nicer to work with.

If I understand your request correctly, this should give you what you want:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
    WeekFields localWeekFields = WeekFields.of(Locale.getDefault());
    LocalDate today = LocalDate.now(ZoneId.of("Europe/Helsinki"));
    Month thisMonth = today.getMonth();
    LocalDate weekStart = today.withDayOfMonth(1)
            .with(TemporalAdjusters.previousOrSame(localWeekFields.getFirstDayOfWeek()));
    // Iterate over weeks
    do {
        System.out.println("Here’s a week:");
        // Iterate over days in week
        LocalDate day = weekStart;
        for (int i = 0; i < 7; i++) {
            System.out.println(day.format(dateFormatter));
            day = day.plusDays(1);
        }
        weekStart = weekStart.plusWeeks(1);
    } while (weekStart.getMonth().equals(thisMonth));

Running this snippet on my computer today it prints the days of 5 weeks, so I will only show you the first and the last lines of output:

Here’s a week:
30-10-2017
31-10-2017
01-11-2017
02-11-2017
…
Here’s a week:
27-11-2017
28-11-2017
29-11-2017
30-11-2017
01-12-2017
02-12-2017
03-12-2017

Since in my locale the week starts on Monday, the above weeks go from Monday to Sunday. If you run the same code in a locale where Sunday is the first day of the week, they should go from Sunday to Saturday.

A little explanation: WeekFields.of(Locale.getDefault()) gives us a WeekFields object of the current locale. We use this a few lines down to determine the first day of the week. Then we query today’s date in your desired time zone — please fill the desired time zone in if you don’t want Europe/Helsinki. To iterate over the weeks, we initialize a LocalDate to the first day of the first week by first finding the first day of this month and then going back to the first day of the week (possibly going into the previous month). To determine which is the first day of the week, we query the WeekFields object that we got a few lines earlier as the one belonging to the current locale (you can fill in a different locale if desired, or just a different WeekFields object).

In the loop over weeks we first print the week and then add one week to weekStart, so we’re ready for next week. The loop condition is that we’re within the current month. To make sure the loop makes its first iteration even if we started on one of the last days of the previous month, I use a do-while loop rather than a while loop.

Will this work on Android?

You certainly can use the modern API on Android too. You need to get ThreeTenABP, the backport of the modern API to Android (that’s ThreeTen for JSR-310 and ABP for Android Backport). It’s all well and thoroughly explained in this question: How to use ThreeTenABP in Android Project.

Upvotes: 1

Related Questions