2

I want to take all the dates in this week and last week .. so I can now get all dates this week and last week

ArrayList<String> week1 = new ArrayList<>(); // this week
ArrayList<String> week2 = new ArrayList<>(); // last week
ArrayList<String> week3 = new ArrayList<>(); // weeks ago
ArrayList<String> week4 = new ArrayList<>(); // weeks ago

Calendar cal = Calendar.getInstance();
    for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        cal.set(Calendar.DAY_OF_WEEK, i);
        week1.add(sdf.format(cal.getTime()));
    }

how to get week2,week3,week4 ?

mustain
  • 23
  • 3
  • I'd just substract 7 days to get to the previous week, then 6 other days to get back to Sunday... – kumesana Jul 06 '18 at 05:54
  • 1
    Your topic sentence and title contradict each other. Do you want 4 weeks or 2 weeks worth of dates? – Tim Biegeleisen Jul 06 '18 at 05:54
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and its equally outdated friend `Calendar`, 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. And better suited for your task. – Ole V.V. Jul 06 '18 at 06:17
  • @TimBiegeleisen 4 week. – mustain Jul 06 '18 at 06:24
  • @OleV.V. Thanks for the suggestion – mustain Jul 06 '18 at 06:27

2 Answers2

1

You can add -1 to Calendar.WEEK_OF_YEAR to get previous WEEK .

 calendar.add(Calendar.WEEK_OF_YEAR,-1);

So final code could be .

 ArrayList<String> week1 = new ArrayList<>(); // this week
    ArrayList<String> week2 = new ArrayList<>(); // last week
    ArrayList<String> week3 = new ArrayList<>(); // weeks ago
    ArrayList<String> week4 = new ArrayList<>(); // weeks ago
    Calendar calendar=Calendar.getInstance();
    for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        calendar.set(Calendar.DAY_OF_WEEK, i);
        week1.add(sdf.format(calendar.getTime()));
    }
    calendar.add(Calendar.WEEK_OF_YEAR,-1);
    for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        calendar.set(Calendar.DAY_OF_WEEK, i);
        week2.add(sdf.format(calendar.getTime()));
    }
    calendar.add(Calendar.WEEK_OF_YEAR,-1);
    for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        calendar.set(Calendar.DAY_OF_WEEK, i);
        week3.add(sdf.format(calendar.getTime()));
    }
    calendar.add(Calendar.WEEK_OF_YEAR,-1);
    for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        calendar.set(Calendar.DAY_OF_WEEK, i);
        week4.add(sdf.format(calendar.getTime()));
    }

Although this seems too much code to do this . There can be an shorter solution for this. You can have a look at Calendar . It has lots of easy methods to manipulate Dates .

ADM
  • 20,406
  • 11
  • 52
  • 83
1
    LocalDate today = LocalDate.now(ZoneId.of("Asia/Harbin"));
    LocalDate weekStart = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
    List<LocalDate> thisWeek = weekStarting(weekStart);
    List<LocalDate> lastWeek = weekStarting(weekStart.minusWeeks(1));
    List<LocalDate> twoWeeksAgo = weekStarting(weekStart.minusWeeks(2));
    List<LocalDate> threeWeeksAgo = weekStarting(weekStart.minusWeeks(3));

    System.out.println("3 weeks ago: " + threeWeeksAgo);
    System.out.println("2 weeks ago: " + twoWeeksAgo);
    System.out.println("Last week:   " + lastWeek);
    System.out.println("This week:   " + thisWeek);

Most of the “magic” happens in my auxiliary method:

private static List<LocalDate> weekStarting(LocalDate date) {
    // Might want to validate that date.getDayOfWeek() == DayOfWeek.SUNDAY
    List<LocalDate> week = new ArrayList<>(7);
    for (int day = 0; day < 7; day++) {
        week.add(date);
        date = date.plusDays(1);
    }
    return week;
}

When I ran the code today, it printed:

3 weeks ago: [2018-06-10, 2018-06-11, 2018-06-12, 2018-06-13, 2018-06-14, 2018-06-15, 2018-06-16]
2 weeks ago: [2018-06-17, 2018-06-18, 2018-06-19, 2018-06-20, 2018-06-21, 2018-06-22, 2018-06-23]
Last week:   [2018-06-24, 2018-06-25, 2018-06-26, 2018-06-27, 2018-06-28, 2018-06-29, 2018-06-30]
This week:   [2018-07-01, 2018-07-02, 2018-07-03, 2018-07-04, 2018-07-05, 2018-07-06, 2018-07-07]

I took it from your code that the first day of the week is Sunday. In many places in the world the week starts on some other day. If you need to take this into account, the code must be modified.

I have stored LocalDate objects in the lists rather than strings. For most purposes this is a better approach. Format the dates only when you need to present them to a user.

Even for Android I am using and suggesting java.time, the modern Java date and time API. Will this work? Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

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

The Calendar and SimpleDateFormat classes that you used (and that come built-in on earlier Android versions too) are long outdated and poorly designed. At least for non-trivial date operations like yours I don’t recommend them.

Links

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