-1

I mean something like this:

28.12.2017

29.12.2017

30.12.2017

31.12.2017

01.01.2018

02.01.2018

And so on I searched the Internet and the closest thing I found was this: link I tried to build a loop that would run all day long but it was irrelevant and I left it fast. I have no ideas on how to do it. Can you please help me?

InziKhan
  • 116
  • 1
  • 8
Menachem Yarhi
  • 63
  • 3
  • 11

2 Answers2

0

I am not sure if there is a single method to call and retrieve the list of dates you wanted, but you can do it with a couple of steps:

  1. Use Calendar.getInstance().set() to set the initial date.
  2. Loop (start date -> end date) and perform the below insie the block:

    a. Increment the date - Calendar.getInstance().add(DATE, 1);

    b. Get the date from the calendar instance as Date or timeInMillies

    c. Use a SimpleDateFormat to convert this to the format you wanted. Add this string to a list or use it anyway you want.

Henry
  • 17,490
  • 7
  • 63
  • 98
0
public String datesBetween (int startDay, int startMonth, int startYear, int endDay, int endMonth, int endYear) {
    int month = startMonth;
    int day = startDay;
    int year = startYear; // start on the first day
    String returnString = ""; // start with an empty list

    while (nextDay(day, month, year, endDay, endMonth, endYear)){ // while the date is not yet the end date
        returnString += day + "-" + month + "-" + year + "\n"; // add the date to the string being returned
        }
    return returnString; // return the string
    }


public int dayMax(int month, int year) {
    switch (month) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12: {
            return 31; // these months have 31 days
            }
        case 4: case 6: case 9: case 11: {
            return 30; // these months have 30 days
            }
        case 2: {
            return 28 + ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 1 : 0;
            // February has 28 plus an extra day if it's a leap year (divisible by 4 but not by 100, or divisible by 400)
            }
        }        
    }


public boolean nextDay(int day, int month, int year, int endDay, int endMonth, int endYear){
    day++; // adds 1 to the day
    if (day > dayMax(month, year)) {
        month++;
        day = 1; // if day too high, add 1 to month and reset day to 1
        }
    if (month > 12) {
        year++;
        month = 1; // if month too high, add 1 to year and reset month
        }
    return year < endYear || (year == endYear && month < endMonth) || (year == endYear && month == endMonth && day < endDay);
    }
J F Fitch
  • 116
  • 1
  • 3