You've made the following errors:
0. This is not C++! I removed the C++ tag from your question.
1. It doesn't make sense to recreate a new calendar instance with every iteration of the loop! Create it outside the loop!
2. Setting the inital time should only occur once, before the loop starts, not inside the loop. Move it outside the loop!
3. You initialize the hour only if isCurrentDay is true. When it's false, what should the starting hour be? You probably need to set some meaningful value.
4. Adding -i to the hour of the day doesn't make any sense. Remove that line!
5. Adding 0 to the hour doesn't do anything. Remove that line!
6. You add the result strings into a container, which is good. But then you print these results inside the loop, which means you keep printing the growing list of strings with every iteration! That's nonsense! Move the output after the loop!
7. You add 15 to n, but then the scope of the loop ends, and n is destroyed because it was created inside the loop! This line has no effect. Either remove the line and calculate the total offset in every loop iteration, or move the declaration of n outside the loop!
Actually you don't need n at all when considering problem 1: if you keep the calender instance alive outside the loop, you can simply increment the time by 15 minutes with every iteration.
8. Why do you print the results inside this function at all? It does return the results, so the caller has the option to print them if needed.
Fixing the above errors gives you this code:
private ArrayList<String> getTimeSet(boolean isCurrentDay) {
ArrayList results = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.HOUR_OF_DAY, 0);
if(!isCurrentDay)
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
for (int i = 0; i < 9; i++) {
String day1 = sdf.format(calendar.getTime());
calendar.add(Calendar.MINUTE, 15);
String day2 = sdf.format(calendar.getTime());
String day = day1 + " - " + day2;
results.add(i, day);
}
return results;
}
}
You can call it like this:
ArrayList<String> results = getTimeSet(true);
System.out.println(results);
Note that there is no need to keep track of the hour: if adding some minutes moves the amount outside the valid range [0,59], then the class will take care of adjusting the hour accordingly.