JamLis :
I'm trying to create an array of times from the current time to a set time, for example; the current time is 15:41, I would like that to be rounded up to the nearest quarter of an hour (15:45) and an array of 15 minute intervals to be created from 15:45 to a specified time lets say 23:30.
I've managed to create an array of times for a 24 hour period in 15 minute intervals and can't seem to get any further forward.
String[] quarterHours = {"00","15","30","45"};
String[] times = new String[24];
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times[i] = "Today " + time;
}
}
The output from the above in a DialogList view within Android looks as follows:
Federico klez Culloca :
You're overwriting the time each cycle of the inner loop. You should use a List<String>
instead and just append without worrying about indexes, like this:
String[] quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}