如何基于1小时间隔获取时间段 [英] How to get Time Slot based on 1hour interval

查看:106
本文介绍了如何基于1小时间隔获取时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:00 AM
10:00 AM
11:00 AM
12:00 PM
13:00 PM
14:00 PM
.....依此类推
21:00 PM

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

,因此一个用户预订了13:00 PM至15:00 PM的广告位,因此其他用户不应该使用该广告位。

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.

代码

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


推荐答案

尝试如下操作:

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中删除,然后更新到服务器,以便下一个
用户尝试获取数据时,它将不会获得第一个选定的用户时隙。

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.

这篇关于如何基于1小时间隔获取时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆