0

I'm wondering how I would compare times in a Java program. For example: If I choose a time block from 09:00-12:00 and set that. If i choose a second time and choose 11:00-13:00 time period, I would want to be able to have an error pop up, but I'm not exactly sure how to set that up.

For example if you are reserving a time at a massage therapy place from 9-12, you can't reserve another one for yourself at 11-1, it wouldn’t make sense. I'm trying to figure out how to put it into code. My noob level is having me think to have a string as a selection method, then parse the string to integer and compare the integer for overlapping? I want to use the Date class though because it seems very useful and I'd like to learn how to use it.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • @PaulRooney I think the OP wants error if the two time intervals overlap. Like detection of meeting conflicts. – peter.petrov Nov 07 '19 at 23:13
  • 3
    Your question is not clear, can you add some input and output example – Ryuzaki L Nov 07 '19 at 23:23
  • @peter.petrov Yes that is exactly what I mean, I didn't really know how to word it because it is a weird question. For example if you are reserving a time at a massage therapy place from 9-12, you can't reserve another one for yourself at 11-1, it wouldnt make sense. I'm trying to figure out how to put it into code. My noob level is having me think to have a string as a selection method, then parse the string to integer and compare the integer for overlapping? I want to use the Date class though because it seems very useful and I'd like to learn how to use it. – Eric Hubbard Nov 07 '19 at 23:28
  • 1
    There is [this](https://stackoverflow.com/questions/17106670/how-to-check-a-timeperiod-is-overlapping-another-time-period-in-java). fwiw you'd do better to use the java.time classes instead of Date. – Paul Rooney Nov 08 '19 at 00:12
  • Possible duplicate of [Determine Whether Two Date Ranges Overlap](https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) – Ole V.V. Nov 08 '19 at 06:49
  • For a time of day like 13:00 you need the `java.time.LocalTime` class. `LocalTime` objects have `isBefore` and `isAfter` methods for your purpose. – Ole V.V. Nov 08 '19 at 06:50
  • 1
    Thank you for providing more information and a very explanatory example. When doing so, please always edit the question and add the information there so we have everything in one place, it’s much nicer. Readers very often will not go through the comments in order to understand your question. – Ole V.V. Nov 08 '19 at 21:22
  • FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. – Basil Bourque Nov 19 '19 at 20:43

1 Answers1

1

LocalTime from java.time

    LocalTime blockStart = LocalTime.of(9, 0);
    LocalTime blockEnd = LocalTime.of(12, 0);
    assert blockEnd.isAfter(blockStart);

    LocalTime newBlockStart = LocalTime.of(11, 0);
    LocalTime newBlockEnd = LocalTime.of(13, 0);
    assert newBlockEnd.isAfter(newBlockStart);

    if (newBlockStart.isBefore(blockEnd) && newBlockEnd.isAfter(blockStart)) {
        System.out.println("" + newBlockStart + '–' + newBlockEnd
                + " overlaps with " + blockStart + '–' + blockEnd);
    }

Output from the snippet is:

11:00–13:00 overlaps with 09:00–12:00

A LocalTime is a time of day without a date. A potential liability is that it cannot take into account if the clock is turned forward or backward, for example when summer time (DST) begins or ends. But if you want nothing that fancy, it’s the correct class to use.

The formula for detecting an overlap is standard. I include a link to a question about it below.

I want to use the Date class though…

Don’t, don’t, please don’t. The Date is poorly designed and long outdated. You are seriously better off not learning how to use that class. Learn to use java.time, the modern Java date and time API. A possible place to start is the first link below.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161