我正在为每周重复发生定期预约功能。我有开始日期,结束日期,复发开始日期,复发结束日期和选定日期(周一,周二,周三,周四......周日)
这是一个例子
Start Date: 15th July 2015
End Date: 18th July 2015
Recurrence Start Date: 20th July 2015
Recurrence End Date: 20th August 2015
Recurrence frequency = weekly
Selected Days(Array storing int values for days_of_week with Sun as 1 and Sat as 7) = Mon, Wed, Sun
根据我需要创建这样的约会: -
Appointment 1 - 20th July 2015 (Mon) - 23rd July 2015(Thu)
............Appointment 2 - 22nd July 2015 (Wed) - 25th July 2015(Sat)
............Appointment 3 - 26th July 2015 (Sun) - 29th July 2015(Wed)
............Appointment 4 - 27th July 2015 (Mon) - 30th July 2015(Thu)
但是你可以看到我需要防止重叠。我试图开发一种算法,以防止这种重叠,而不知道实际的日子。
所以基本上没有区别。开始日期和结束日期之间的天数必须大于数组的两个连续索引之间的差异。
我遇到问题,因为Sun - Wed ( 1 - 4)
会给我一个负数,所以比较结果日期和结束日期( end date - start date)
之间的差异将小于。{/ p>
这是我到目前为止所做的: -
Calendar e = Calendar.getInstance();
Calendar f = Calendar.getInstance();
e.setTime(sStartDate);
f.setTime(estSignIn);
long diffInDays = ((estSignIn.getTime() - sStartDate.getTime())
/ (1000 * 60 * 60 * 24) );
for(int j=0; j < localSelectedDays.length - 1 ; j++)
{
e.set(Calendar.DAY_OF_WEEK, localSelectedDays[j]);
f.set(Calendar.DAY_OF_WEEK, localSelectedDays[j +1]);
int x = e.get(Calendar.DAY_OF_WEEK);
int y = f.get(Calendar.DAY_OF_WEEK);
if ((y - x) <= diffInDays)
{
System.out.println("ERROR" + "Y:" + y + "x" + x);
}
}
答案 0 :(得分:0)
如果只想要天数差异,可以使用Math.abs()。
我个人不会将索引号用作算法的一部分,因为有一天你可能想要改变数据结构,但这是主观的。
答案 1 :(得分:0)
但是你可以看到我需要防止重叠。我曾是 试图开发一种算法,以防止这种重叠 实际上知道实际的日子
我已根据您的要求编写了一些代码。在我看来,使用Calendar或日期类after
&amp; before
方法比较输入日期,而不是使用数组进行比较。
创建一个AppointmentDetails类,它将存储约会详细信息。 您可以在其中添加额外的属性,例如人名等。
public class AppointmentDetails {
private Calendar startDate;
private Calendar endDate;
//getters & setters
public String toString()
{
return "startDate "+ this.getStartDate() + "endDate " + this.getEndDate();
}
在客户端类中,将约会详细信息存储在Hashmap中
public class AppointmentClient {
public static void main(String[] arg)
{
System.out.println("Get the appointment");
//Sample data input for the first appointment
Calendar startDate = new GregorianCalendar(2015, 1, 6);
Calendar endDate = new GregorianCalendar(2015, 1, 9);
AppointmentDetails details = new AppointmentDetails();
details.setStartDate(startDate);
details.setEndDate(endDate);
//Details added to the hashmap with key as appointment & value as the class holding appointment details
Map<String,AppointmentDetails> map = new HashMap<String,AppointmentDetails>();
map.put("Appointment 1", details);
//Dates for new appointment
Calendar startDate2 = new GregorianCalendar(2015, 1, 7);
Calendar endDate2 = new GregorianCalendar(2015, 1, 9);
//logic for validating if the appointment on the input date is already booked
for(Map.Entry<String, AppointmentDetails> appDtl:map.entrySet())
{
System.out.println("Inside loop");
//Every time the loop iterates it will return the respective appointment
AppointmentDetails apptDetail = appDtl.getValue();
System.out.println(apptDetail);
if(((apptDetail.getStartDate().equals(startDate2))||(apptDetail.getEndDate().equals(endDate2)))||
(apptDetail.getStartDate().after(startDate2))&&(apptDetail.getEndDate().before(endDate2)))
{
System.out.println("Your appointment is overlapping with the existing appointments");
break;
}
}
}
}
P.S这只是一个基本代码,您可以进一步定制它以提高效率和效率。进一步的逻辑。