0

I have two MyDate objects.

class MyDate {

    public int hours, minutes;

    MyDate(int hours, int minutes) {
        this.hours = hours;
        this.minutes = minutes;
    }
}

MyDate startTime;
MyDate endTime;

and i have a timestamp as a third MyDate object.

MyDate currentTime;

I want to know is currentTimeStamp included or not in interval endDate - startDate

[------------[startTime]------------[currentTime]-----------------[endTime]--------------]

I see three combinations of time :

  • 23:00 - 08:00 (07:00 - true)
  • 12:00 - 13:00 (07:00 - false)
  • 23:59 - 23:58 (07:00 - true)

Explain third case :

[-----------------(23:59)(00:00 - new Day)--(07:00)----------(23:58)--(00:00 - new Day)]

How i can check is currentTimeStamp included or not in interval endDate - startDate?

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • Is there a reason you are not using a well-tested time library like Jodatime, and implementing your own dates instead? – Afforess Oct 25 '14 at 23:00
  • @Afforess i want implement this withour any third party libraries. This is a alghorithmic issue. – Sergey Shustikov Oct 25 '14 at 23:02
  • To figure out if a time is inside an interval you need to check if first if the date is _after_ the start time and _before_ the end time. If both conditions are true, then the date is inside the interval. Create two methods, one to check whether a time is before another time, and the other to check if a time after another time, then the problem becomes simple. – Afforess Oct 25 '14 at 23:05
  • @Afforess Reproduce this in code. Give me example. – Sergey Shustikov Oct 25 '14 at 23:06
  • Can you use Java 8? This is now built-in. – Elliott Frisch Oct 25 '14 at 23:09
  • Is this a homework assignment? If not, rolling your own date-time library is an unwise waste of your time. Search on "Joda" or "java.time". – Basil Bourque Oct 26 '14 at 18:02
  • Regarding Afforess’ comment, a better way to define and compare spans of time is with the **"Half-Open" approach**. This means the beginning is *inclusive* while the ending is *exclusive*. So comparison operators are `>=` and `<`. Search StackOverflow for "half-open" to learn more. – Basil Bourque Oct 26 '14 at 18:07
  • possible duplicate of [Java: how do I check if a Date is within a certain range?](http://stackoverflow.com/questions/494180/java-how-do-i-check-if-a-date-is-within-a-certain-range) – Basil Bourque Oct 26 '14 at 18:11
  • Just noticed you want time-only comparisons without date portion. Search for "LocalTime". For example, this [Question](http://stackoverflow.com/q/22310329). – Basil Bourque Oct 26 '14 at 18:15

1 Answers1

0

Reposting my comment, with solution included.

To figure out if a time is inside an interval you need to check if first if the date is after the start time and before the end time. If both conditions are true, then the date is inside the interval. Create two methods, one to check whether a time is before another time, and the other to check if a time after another time, then the problem becomes simple.

class MyDate {
    public int hours, minutes;

    MyDate(int hours, int minutes) {
        this.hours = hours;
        this.minutes = minutes;
    }

    public boolean isBefore(MyDate date) {
        if (date.hours < hours) {
            return true;
        }
        if (date.hours == hours) {
            return date.minutes < minutes;
        }
        return false;
    }

    public boolean isAfter(MyDate date) {
        if (date.hours > hours) {
            return true;
        }
        if (date.hours == hours) {
            return date.minutes > minutes;
        }
        return false;
    }

    public static boolean isBetween(MyDate intervalStart, MyDate intervalEnd, MyDate time) {
        return time.isAfter(intervalStart) && time.isBefore(intervalEnd);
    }
}

Really though, time and date code like this should not exist out of homework exercises. There are a vast amount of edge-cases when dealing with dates and times, and they are better left to well-tested libraries, like JodaTime.

Afforess
  • 894
  • 7
  • 15
  • This code doesn't worked on time 23:00 - 08:00 with timestamp 07:00 – Sergey Shustikov Oct 26 '14 at 10:46
  • Right, because your time spans 2 days, which you can not represent with only hours and minutes. This is exactly why you should leave implementations of date/time code to well-tested libraries. – Afforess Oct 26 '14 at 18:49