如何基于1小时间隔获取时间段

问题描述 投票:1回答:2

我想将时隙存储在arraylist中。我有开始时间和结束时间。根据开始时间,应该创建一个时隙。例如,如果开始时间为09:00 AM,结束时间为21:00 PM,则应将其添加到arraylist中,如下所示

09:00 AM10:00 AM11:00 AM12:00 PM下午13:00下午14:00.....等等21:00 PM

因此,一个用户预订了13:00 PM到15:00 PM的广告位,因此另一用户不应该使用该广告位,而其他广告位应该可用。如何将已经预订的时间与新阵列列表进行比较。

代码

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

    }


}

时隙模型类

public class TimeSlot {

public String time;
 public String isAvailable;
}
java android date datetime java-time
2个回答
1
投票

尝试这样的事情:

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

这将为您提供每个小时的时隙,将其添加到arraylist中,并且当任何用户选择时间后,将其从arrayList中删除并更新到服务器,以便在下一次用户尝试获取数据将不会获得第一个选定的用户时隙。


0
投票

尝试一下:

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

}
© www.soinside.com 2019 - 2024. All rights reserved.