Search code examples
javaandroiddatesimpledateformat

How to load array of dates from a given range?


I'm looking for the best way to populate a range of dates in my recyclerview. To elucidate, I want to fetch the all the dates from the current date to next seven days like if today is 29-07-2020 I want to list all dates from 29-07-2020 to 04-07-2020. Now in my recycler I want to get the day date and month name. I tried the below code and it list the dates as i wish but I'm unsure how to get the day date and month individually.

List<String> dateList = new ArrayList<String>();
    Calendar calendar = Calendar.getInstance();
    for (int i=0; i<31; i++) {
        calendar.add(Calendar.DATE, 1);
        SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
        dateList.add(curFormater.format(calendar.getTime()));

        Log.i("Date" + i , dateList.get(i));
    }

Solution

  • I recommend you switch from the outdated legacy date-time API to the modern date-time API.

    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
            LocalDate today = LocalDate.now();// Same as LocalDate.now(ZoneId.systemDefault());
            // I suggest you use the following variant and apply the required time-zone
            // LocalDate today=LocalDate.now(ZoneId.of("Etc/UTC"));
    
            List<String> dateList = new ArrayList<String>();
    
            // Define the format
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM");
    
            // Iterate 7-times starting today with date incrementing by 1 after each iteration
            for (LocalDate date = today; date.isBefore(today.plusDays(7)); date = date.plusDays(1)) {
                dateList.add(date.format(formatter));
            }
    
            // Display the content of dateList
            dateList.forEach(System.out::println);
        }
    }
    

    Output:

    Monday, August
    Tuesday, August
    Wednesday, August
    Thursday, August
    Friday, August
    Saturday, August
    Sunday, August
    

    If the version of your Android is not compatible with Java-8 yet, you can backport using ThreeTen-Backport. You may also need help to set it up.

    However, if you want to stick to the legacy date-time API, do it as follows:

    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
            Calendar calendar = Calendar.getInstance();
            List<String> dateList = new ArrayList<>();
    
            // Define the format for only day-name and month-name 
            SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM");
    
            // Iterate 7-times starting today with date incrementing by 1 after each iteration
            for (int i = 1; i <= 7; i++) {
                dateList.add(curFormater.format(calendar.getTime()));
                calendar.add(Calendar.DATE, 1);
            }
    
            // Display the content of dateList
            dateList.forEach(System.out::println);
        }
    }
    

    Output:

    Monday, August
    Tuesday, August
    Wednesday, August
    Thursday, August
    Friday, August
    Saturday, August
    Sunday, August