-2

I want to create spinner which has

8:00-8:30 AM
8:30-9:00 AM
9:00-9:30 AM

time slot I done with the ArrayList like

8:00 AM
9:00 AM
10:00 AM

and so on.

I got the arraylist like

ArrayList[08:00:AM, 09:00:AM, 10:00:AM, 11:00:AM, 12:00:PM, 13:00:PM, 14:00:PM, 15:00:PM, 16:00:PM, 17:00:PM, 18:00:PM, 19:00:PM, 20:00:PM, 21:00:PM]

This is my code:

public class MainActivity extends AppCompatActivity {

    ArrayList<String> hours = new ArrayList<String>();

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

        initCustomTimeSpinner();
    }

    private void initCustomTimeSpinner() {

        Spinner spinnerCustom = (Spinner) findViewById(R.id.spinner2);

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:a");

        SimpleDateFormat startHourFormat = new SimpleDateFormat("HH");
        cal.set(Calendar.HOUR_OF_DAY, 8);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        int startHour = Integer.parseInt(startHourFormat.format(cal.getTime()));
        Log.e("startHour", Integer.toString(startHour));

        Log.e("withot format Starttime", cal.getTime().toString());

        SimpleDateFormat endHourFormat = new SimpleDateFormat("HH");
        cal.set(Calendar.HOUR_OF_DAY, 21);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        int endHour = Integer.parseInt(endHourFormat.format(cal.getTime()));
        Log.e("endHour", Integer.toString(endHour));

        Log.e("withot format End time", cal.getTime().toString());

        String test = sdf.format(cal.getTime());
        Log.e("TEST", test);

        for (int i = startHour; i <= endHour; i++) {

            cal.set(Calendar.HOUR_OF_DAY, i);
            hours.add(sdf.format(cal.getTime()));
            System.out.println("ArrayList" + hours);
        }
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Where exactly are you having difficulty? – Joe C Oct 02 '17 at 19:09
  • got stuck in creating half an hour interval I got list like 8:00 AM,9:00 AM but as I mentioned I want to create 8:00-8:30 AM in first index second index 8:30-9:00 AM and so on on further index – Shyamal Patel Oct 02 '17 at 19:12
  • What have you tried? Where exactly are you failing? You seem to be asking us to write it for you, which we don't do here. – Joe C Oct 02 '17 at 19:14
  • @JoeC I am just asking for logic I tried and got list like 8:00 AM,9:00 AM and so on if you want I can share my github link – Shyamal Patel Oct 02 '17 at 19:17
  • Your logic is almost right, you just need to learn [how to properly use a debugger](https://stackoverflow.com/q/25385173/7605325) - then you'll notice that instead of `for(...) { cal.set(Calendar.HOUR_OF_DAY, i)` (which loops for whole hours) you should do `cal.add(Calendar.MINUTE, 30)` (add 30 minutes until endDate is reached) just like in [Juan's answer](https://stackoverflow.com/a/46532434/7605325). Relevant link: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –  Oct 02 '17 at 19:41
  • Similar/duplicate Question: [How do I set half hour intervals on a TimePicker in Android?](https://stackoverflow.com/q/33299753/642706). – Basil Bourque Oct 02 '17 at 23:55
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & 7 in [*ThreeTen-Backport*](http://www.threeten.org/threetenbp/). Further adapted for Android in the [*ThreeTenABP*](https://github.com/JakeWharton/ThreeTenABP) project. See [Answer by Andreas](https://stackoverflow.com/a/46532206/642706) – Basil Bourque Oct 02 '17 at 23:59

1 Answers1

3

Here is simple example of building time-slots using Java 8 Time API:

public static void printTimeSlots(LocalTime startTime, LocalTime endTime, int slotSizeInMinutes) {
    for (LocalTime time = startTime, nextTime; time.isBefore(endTime); time = nextTime) {
        nextTime = time.plusMinutes(slotSizeInMinutes);
        if (nextTime.isAfter(endTime))
            break; // time slot crosses end time
        System.out.println(time + "-" + nextTime);
    }
}

Test

printTimeSlots(LocalTime.parse("08:00"), LocalTime.parse("17:00"), 30);

Output

08:00-08:30
08:30-09:00
09:00-09:30
09:30-10:00
10:00-10:30
10:30-11:00
11:00-11:30
11:30-12:00
12:00-12:30
12:30-13:00
13:00-13:30
13:30-14:00
14:00-14:30
14:30-15:00
15:00-15:30
15:30-16:00
16:00-16:30
16:30-17:00

Test #2, demonstrating misaligned end time

printTimeSlots(LocalTime.parse("10:05"), LocalTime.parse("11:55"), 15);
10:05-10:20
10:20-10:35
10:35-10:50
10:50-11:05
11:05-11:20
11:20-11:35
11:35-11:50

Or course, if you don't want to be limited to slot size in minutes, you can using TemporalAmount instead of number of minutes:

public static void printTimeSlots(LocalTime startTime, LocalTime endTime, TemporalAmount amountToAdd) {
    for (LocalTime time = startTime, nextTime; time.isBefore(endTime); time = nextTime) {
        nextTime = time.plus(amountToAdd);
        if (nextTime.isAfter(endTime))
            break; // time slot crosses end time
        System.out.println(time + "-" + nextTime);
    }
}

Test

printTimeSlots(LocalTime.parse("10:05:15"),
               LocalTime.parse("11:55:00"),
               Duration.ofMinutes(15).plusSeconds(25));
10:05:15-10:20:40
10:20:40-10:36:05
10:36:05-10:51:30
10:51:30-11:06:55
11:06:55-11:22:20
11:22:20-11:37:45
11:37:45-11:53:10
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • @JuanCarlosMendoza For Android, that should actually be [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) – Andreas Oct 11 '17 at 19:17