Java 比较同一数组的元素

Java 比较同一数组的元素,java,arrays,calendar,Java,Arrays,Calendar,我正在使用定期约会功能进行每周定期约会。我有开始日期、结束日期、定期开始日期、定期结束日期和选定的日期(周一、周二、周三、周四…周日) 这里有一个例子 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

我正在使用定期约会功能进行每周定期约会。我有开始日期、结束日期、定期开始日期、定期结束日期和选定的日期(周一、周二、周三、周四…周日)

这里有一个例子

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)
会给我一个负数,所以比较结束日期和开始日期
(结束日期-开始日期)

这就是我到目前为止所做的:-

                        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);
                                  }
                        }
Calendar e=Calendar.getInstance();
Calendar f=Calendar.getInstance();
e、 设定时间(sStartDate);
f、 设定时间(estSignIn);
long diffInDays=((estSignIn.getTime()-sStartDate.getTime())
/ (1000 * 60 * 60 * 24) );
对于(int j=0;j
我个人不会将索引数用作算法的一部分,因为有一天您可能会想更改数据结构,但这是主观的。

如果您只想以天为单位计算差异,可以使用Math.abs()

我个人不会使用索引号作为算法的一部分,因为有一天您可能想要更改数据结构,但这是主观的

但正如你所看到的,我需要防止一些重叠 试图开发一种算法来防止这种重叠,而不需要 实际上知道实际的日子

我已经根据您的要求编写了一些代码。我认为使用or Date类
after
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;
                }
            }
         }
      }
公共类任命客户端{
公共静态void main(字符串[]arg)
{
System.out.println(“获得约会”);
//第一次约会的示例数据输入
日历开始日期=新的公历开始日期(2015年1月6日);
日历结束日期=新的公历日历日(2015年1月9日);
AppointmentDetails=新的AppointmentDetails();
详细信息。设置开始日期(开始日期);
详细信息。setEndDate(endDate);
//详细信息添加到hashmap中,key作为appointment&value作为持有约会详细信息的类
Map Map=newhashmap();
地图放置(“约会1”,详情);
//新任命日期
日历开始日期2=新的格里高利安日历(2015年1月7日);
日历结束日期2=新的格里高利安日历(2015年1月9日);
//用于验证输入日期的约会是否已预订的逻辑
对于(Map.Entry appDtl:Map.entrySet())
{
System.out.println(“内部循环”);
//每次循环迭代时,它都会返回相应的约会
AppointmentDetails apptDetail=appDtl.getValue();
System.out.println(apptDetail);
如果(((apptDetail.getStartDate().equals(startDate2))||(apptDetail.getEndDate().equals(endDate2)))||
(apptDetail.getStartDate().after(startDate2))&&(apptDetail.getEndDate().before(endDate2)))
{
System.out.println(“您的约会与现有约会重叠”);
打破
}
}
}
}
这只是一个基本代码,您可以进一步定制它以提高效率和逻辑性

但正如你所看到的,我需要防止一些重叠 试图开发一种算法来防止这种重叠,而不需要 实际上知道实际的日子

我已经根据您的要求编写了一些代码。我认为使用or Date类
after
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;
                }
            }
         }
      }
公共类任命客户端{
公共静态void main(字符串[]arg)
{
System.out.println(“获得约会”);
//第一次约会的示例数据输入
日历开始日期=新的公历开始日期(2015年1月6日);
日历结束日期=新的公历日历日(2015年1月9日);
AppointmentDetails=新的AppointmentDetails();
详细信息。设置开始日期(开始日期);
详细信息。setEndDate(endDate);
//详细信息添加到hashmap中,key作为appointment&value作为持有约会详细信息的类
Map Map=newhashmap();
地图放置(“约会1”,详情);
//新任命日期
日历开始日期2=新的格里高利安日历(2015年1月7日);
日历结束日期2=新的格里高利安日历(2015年,1