简体   繁体   中英

Why date comparison fails?

I want to check if my events dose overlap. I have saved start time and end time in date format. My attempt is to check whether dates overlap or not by compare to. But it fails sometimes.

Most it fails at 11pm to 12 am values and 12 :00 am to 1:00 am values. Sometimes fails in between also for another time periods.

As I have a time period stored as ,

datefrom -- Mon mar 14 23:00:00 dateto -- Mon mar 14 00:00:00

and now I tried to add time period as :

checkFromDate -- Mon mar 14 12:14:00 checkToDate -- Mon mar 14 11:14:00

and it returns true. Shows event exists though it dose not.

Date format is:

        df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");

Is anything wrong in date format??

checkOverlapping function

public void checkOverlappingEvents()
{

    List<EventData> checkOverlappingEvents = new ArrayList<>();

    EventTableHelper eventTableHelper = new EventTableHelper(AddEventActivity.this);


    switch (dayOfWeek)
    {
        case "Mon":


    checkOverlappingEvents = eventTableHelper.getAllEvents("Mon");


    for (EventData e : checkOverlappingEvents)
    {

        df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");

        Date checkFromDate = new Date();
        Date checkToDate = new Date();


        try {

            checkFromDate = df.parse(e.getFromDate());
            checkToDate = df.parse(e.getToDate());

        } catch (ParseException ex) {

        }


    //    int startHours = datefrom.getHours();
    //    int endHours = dateto.getMinutes();

    //    String startTime = checkFromDate.getHours() + ":" + checkFromDate.getMinutes();
    //    String endTime = checkToDate.getHours() + ":" + checkToDate.getMinutes();

     //   String startDate = datefrom.getHours() + ":" + datefrom.getMinutes();
      //  String endDate = dateto.getHours() + ":" + dateto.getMinutes();

        if(datefrom.compareTo(checkToDate) == checkFromDate.compareTo(dateto))
        {
            overlapEvents = true;
            count ++;
            Log.d("count",String.valueOf(count));

        }

    }

This dose not help so I thought to only compare hours and minutes of date. My attempt was getting hours and minutes from date and compare them , it's in comments. But it needs some other logic.

Can anyone help me with this please?

EDIT :

As I did test again, I found that if once the condition returns true, then second time for another time period also it shows true though its not true.

Like now, I have a event of 12:50pm - 1:50pm and I tried to add another event of time 12:00pm - 1:00 pm and that returned true,, count is 1. This is right.

But again if I try to change the time without going back , without refreshing an activity, I tried to change time as 12:00pm to 12:30pm for this also it returned true, count is 1 as this should not be true there is no event of this time period.

Again if I go back and again If I try to add event of 12:00pm to 12:30pm it returned false, it get added successfully. Now this I don't know what's going wrong about?

I think I need to refresh the list where I am storing the compared events. How can I do this and where should I do this?

Thank you.

Maybe I'm misunderstanding the question, but the time 00:00:00 is BEFORE 23:00:00. I tested it to make sure:

SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss");
    System.out.println(df.parse("Mon mar 14 23:00:00").getTime()); // 6296400000
    System.out.println(df.parse("Mon mar 14 00:00:00").getTime()); // 6213600000

Because of this, the dates Mon mar 14 12:14:00 -- Mon mar 14 11:14:00 would exist between Mon mar 14 00:00:00 -- Mon mar 14 23:00:00.

So the code might be correct, your test is just confused.

Let's say your dates are like this :

Mon mar 14 12:14:00

your format should be like this

SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss");

You just entered one E for your dayOfWeek

EDIT :

SimpleDateFormat acts like a parser. every parameter you use in it, indicate the way the parser should behave to it.

Let's back to your example i mean >>> Mon mar 14 12:14:00

If you use this :

SimpleDateFormat df = new SimpleDateFormat("E MMM dd HH:mm:ss");

You will tell the parser that only 1st character of the String indicates the DayNameInTheWeek. in our example it takes M as its indicator for day of week. which is not identical for Date. What day of week M refer to ?!?!?!?! The supported formats are the parameters in Example column:

在此处输入图片说明

As you see the least acceptable length for day of week is 3 like TEU,MON,FRI,etc...

If you have two date ranges, start1 to end1 , and start2 to end2 , and you want to see if they overlap, try to look at it from the other side.

Check if the don't overlap. If one of them ends before the other starts, they don't overlap:

end1 <= start2  or  end2 <= start1

Negating that, to see if they overlap, give us:

end1 > start2  and  end2 > start1

Or, in Java with Date objects, it becomes:

if (end1.after(start2) && end2.after(start1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM