2

I want to store time slot in the arraylist. i have start time and end time. based on start time it should create time slot. For example if start time is 09:00AM and end time is 21:00PM then it should add into arraylist like below

09:00AM 10:00AM 11:00AM 12:00PM 13:00PM 14:00PM ..... so on 21:00PM

so one user books 13:00PM to 15:00PM slots so it should not be available to another user and other slot should be available. how to compare already booking time with new array list.

Code

    private void getStartHourArray() {

    times = new ArrayList<TimeSlot>();
    Calendar calender = Calendar.getInstance();

    calender.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));

    int ti = calender.get(Calendar.HOUR_OF_DAY);
    int minutes = calender.get(Calendar.MINUTE);

    System.out.println(minutes);

    String[] quarterHours = {
            "00",

            "30",

    };
    boolean isflag = false;


    times = new ArrayList<>();

    for (int i = 9; i < 22; i++) {

        if (ti > 8) {
            for (int j = 0; j < 2; j++) {

                if ((i == ti && minutes < Integer.parseInt(quarterHours[j])) || (i != ti) || isflag == true) {

                    isflag = true;
                    String time = i + ":" + quarterHours[j];
                    if (i < 10) {
                        time = "0" + time;
                    }
                    String hourFormat = i + ":" + quarterHours[j];
                    if (i < 12) {
                        hourFormat = time + " AM";
                    } else
                        hourFormat = time + " PM";

                    TimeSlot t = new TimeSlot();
                    t.time = hourFormat;
                    t.isAvailable = "Available";
                    times.add(t);
                }
            }
        }

    }

    if (times != null) {
        load.setVisibility(View.GONE);

    }


}

Time Slot model class

public class TimeSlot {

public String time;
 public String isAvailable;
}
Karthick Jai
  • 61
  • 1
  • 11
  • Please add some code to show how the start time and end time is entered. How the booking is done. Provide some minimal code – Abhimanyu Jan 05 '20 at 06:15
  • @Abhimanyu have added code please review it – Karthick Jai Jan 05 '20 at 06:22
  • 1
    For such non-trivial work with time I suggest that you don’t rely on the `Calendar` class. It’s poorly designed and long outdated. And under all circumstances you shouldn’t hand format the time strings the way you do. Instead add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `LocalTime` and `DateTimeFormatter` from java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 05 '20 at 07:53
  • 1
    This question seems to be asked a number of times with variations, [here](https://stackoverflow.com/questions/59521462/how-to-set-time-slot-in-android), [here](https://stackoverflow.com/questions/59525779/how-to-add-time-slot-in-recyclerview-android), [here](https://stackoverflow.com/questions/59573807/how-to-get-time-in-slot-type-in-android), [here](https://stackoverflow.com/questions/59597354/how-to-get-time-slot-interval-of-1hour-based-two-times-android) and [here](https://stackoverflow.com/questions/59597453/how-to-get-time-slot-based-on-1hour-interval). – Ole V.V. Jan 05 '20 at 12:26

2 Answers2

6

Try something like this :

String firstDate = "26/02/2019";
String firstTime = "00:00 AM";
String secondDate = "26/02/2019";
String secondTime = "12:00 PM";

String format = "dd/MM/yyyy hh:mm a";

SimpleDateFormat sdf = new SimpleDateFormat(format);

Date dateObj1 = sdf.parse(firstDate + " " + firstTime);
Date dateObj2 = sdf.parse(secondDate + " " + secondTime);
System.out.println("Date Start: "+dateObj1);
System.out.println("Date End: "+dateObj2);

long dif = dateObj1.getTime(); 
while (dif < dateObj2.getTime()) {
    Date slot = new Date(dif);
    System.out.println("Hour Slot --->" + slot);
    dif += 3600000;
}

This will give you a time slot for each hour, add this in ArrayList and when any user select time then remove that from ArrayList and update to the server so when next user tries to get data it won't get the first selected user time slot.

6006604
  • 7,577
  • 1
  • 22
  • 30
haresh
  • 1,424
  • 2
  • 12
  • 18
  • 1
    Thank you so much. Got desired output. and need another help. I will ask a new question with some code. – Karthick Jai Jan 05 '20 at 07:52
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 05 '20 at 07:54
1

try this:

import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

public class PlayGround {

    private Map<LocalTime, Boolean> slots = new HashMap();

    public static void main(String[] args) {
        PlayGround client = new PlayGround();
        client.initializeSlots();

        client.allocateSlots("10:00", "13:00");
        //this shouldn't be available
        client.allocateSlots("11:00", "12:00");
        //not sure if u want this to be available. since it is start when the 1st just finished. 
        client.allocateSlots("13:00", "15:00");
        client.allocateSlots("16:00", "18:00");
    }

    private void initializeSlots() {

        LocalTime time = LocalTime.of(9, 0);
        slots.put(time, true);
        for (int i = 1; i < 24; i++) {
            slots.put(time.plusHours(i), true);
        }
    }

    private void allocateSlots(String strTime, String edTime) {
        LocalTime startTime = LocalTime.parse(strTime);
        LocalTime endTime = LocalTime.parse(edTime);

        while (startTime.isBefore(endTime)) {
            //check if the time slots between start and end time are available
            if (!slots.get(startTime) || !slots.get(endTime)) {
                System.out.println("slots not available" + " start time: " + strTime + " end time: " + edTime);
                return;
            }
            startTime = startTime.plusHours(1);
            endTime = endTime.minusHours(1);
        }

        System.out.println("slots are available" + " start time: " + strTime + " end time: " + edTime);
        //then here u can mark all slots between to unavailable.
        startTime = LocalTime.parse(strTime);
        endTime = LocalTime.parse(edTime);
        while (startTime.isBefore(endTime)) {
            slots.put(startTime, false);
            slots.put(endTime, false);
            startTime = startTime.plusHours(1);
            endTime = endTime.minusHours(1);
        }
    }

}
Aheza Desire
  • 509
  • 5
  • 4
  • 2
    It looks good. Some explanation of how it works and how it answers the question would be wonderful. – Ole V.V. Jan 05 '20 at 07:59