Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, I'd use <a href="http://joda-time.sf.net" rel="nofollow">Joda Time</a> and its <code>LocalTime</code> type to represent the times. You can then do:</p> <pre><code>static boolean isInRange(LocalTime start, LocalTime end, LocalTime value) { // Avoid equality problems - see description if (value.equals(start) || value.equals(end)) { return true; } if (start.compareTo(end) &gt; 0) { return !isInRange(end, start, value); } return start.compareTo(value) &lt; 0 &amp;&amp; value.compareTo(end) &lt; 0; } </code></pre> <p>In other words, you'd generally only end up doing the final comparison with <code>value</code> when you'd got a start/end range where <code>start &lt;= end</code> - but if you start off with the other way round, then you just invert the result of doing the reverse comparison.</p> <p>In other words, something is in the range (10pm, 2am) if it's <em>not</em> in the range (2am, 10pm).</p> <p>Alternatively, you could avoid the recursive call:</p> <pre><code>static boolean isInRange(LocalTime start, LocalTime end, LocalTime value) { return start.compareTo(end) &gt; 0 // Reversed: if it's earlier than the end point, or later than the start ? value.compareTo(end) &lt;= 0 || start.compareTo(value) &lt;= 0 : start.compareTo(value) &lt;= 0 &amp;&amp; value.compareTo(end) &lt;= 0; } </code></pre> <p>The tricky bit is equality. The code above assumes that the start/end points are both inclusive, either way round. If you want anything else, you'll need to think very carefully about every situation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload