0

Is it possible to list the all weeks/date given two date range for example:

Date from 1/1/2013 to 1/1/2020 result will be:

  • 1-7,2013
  • 8-14,2013
  • 15-21,2013 and soon til 2020 and same with month.
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

0

Please try out this for the case of weeks(check if you can optimize).

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // to provide month range dynamically

    Calendar calendar = Calendar.getInstance();
    Date minDate = calendar.getTime();
    calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
    Date maxDate = calendar.getTime();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    String startDate = dateFormat.format(minDate);
    String endDate = dateFormat.format(maxDate);

    List<Date> dates = getDates(startDate, endDate); // to get dates between range
    int prevIdentifier = 0;
    int identifier;
    String initDate, finalDate;
    List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates); 
    SimpleDateFormat dformatter = new SimpleDateFormat("dd");
    SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
    initDate = dformatter.format(weekDays.get(0).getDate());
    finalDate = dformatter.format(weekDays.get(0).getDate());
    String outputData = "";
    for (WeekDay weekDay : weekDays) {
        identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
        if (prevIdentifier != 0 && identifier != prevIdentifier) {
            if (outputData.equalsIgnoreCase(""))
                outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
            else
                outputData += "  *  " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
            initDate = dformatter.format(weekDay.getDate());
        } else {
            finalDate = dformatter.format(weekDay.getDate());
        }
        prevIdentifier = identifier;
    }
    System.out.println("OUTPUT DATA :" + outputData);

}

 public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
    List<WeekDay> listOfWeeks = new ArrayList<>();
    WeekDay weekDay;
    for (Date date : listOfDates) {
        weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
        listOfWeeks.add(weekDay);
    }
    return listOfWeeks;
}

public class WeekDay {

    Date date;
    String weekIdentifier;

    public WeekDay(Date Date, String WeekIdentifier) {
        this.date = Date;
        this.weekIdentifier = WeekIdentifier;
    }

    public Date getDate() {
        return date;
    }

    public String getWeekIdentifier() {
        return weekIdentifier;
    }

}

private static List<Date> getDates(String dateString1, String dateString2) {
    ArrayList<Date> dates = new ArrayList<Date>();
    DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");

    Date date1 = null;
    Date date2 = null;

    try {
        date1 = df1.parse(dateString1);
        date2 = df1.parse(dateString2);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);


    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);

    while (!cal1.after(cal2)) {
        dates.add(cal1.getTime());
        cal1.add(Calendar.DATE, 1);
    }
    return dates;
}
  • You are using the badly designed and long outdated date and time classes (`Calendar`, `Date`, `SimpleDateFormat` and `DateFormat`). Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). While someone might use the old-fashioned classes, please at least mention that there is an option of a better solution. One can use java.time on not new Android, you will just need [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Nov 20 '18 at 09:49
  • This was something similar I have used in previous works and I am not familiar with the new update. So I mentioned to "check if he can optimize". Thanks for your update @OleV.V. – Pooja Rajendran C Nov 20 '18 at 10:18
0

java.time

I would use a LocalDate for start date (e.g., 1/1/2013) and one for end date (1/1/2020). To represent the period you want (week, month or year) I might use either the appropriate ChronoUnit constant or — more flexibly — a Period. The mentioned classes are from java.time, the modern Java date and time API. A pretty simple loop will iterate through your start dates (Jan 1, Jan 8, Jan 15, etc.). Subtract 1 from each start date (except the first) to get the end dates (Jan 8 minus 1 day gives Jan 7, etc.).

Question: Can I use java.time on Android?

Yes, java.time works nicely on Android devices. It just requires at least Java 6.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161