How to get Time Slot based on 1hour interval

允我心安 提交于 2020-01-14 03:39:05

问题


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;
}

回答1:


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 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 try to get data it won't get first selected user time slot.




回答2:


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);
        }
    }

}


来源:https://stackoverflow.com/questions/59597453/how-to-get-time-slot-based-on-1hour-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!