title: Compare two intervals in case of equal
body: <p>I am trying to compare two intervals which have <code>dateFrom</code> and <code>dateTo</code> to the <code>filterFrom</code>and <code>filterTo</code> is there a better way to compare two intervals with the border? Also in case <code>dateFrom</code> has the same date as <code>filterFrom</code> or <code>dateTo</code> has the same date as <code>filterTo</code> the method should return true.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  
  public class DateCompare {
  
      public static void main(String[] args) {
  
          try {
              DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              Date filterFrom = dateFormat.parse("2017-01-02 08:00:00.0");
              Date filterTo = dateFormat.parse("2017-01-02 09:00:00.0");
  
              Date dateFrom = dateFormat.parse("2017-01-02 08:40:00.0");
              Date dateTo = dateFormat.parse("2017-01-02 09:00:00.0");
  
              boolean value = DateCompare.compareToIntervales(filterFrom, filterTo, dateFrom, dateTo);
              System.out.println("value: " + value);
  
          } catch (ParseException e) {
  
              e.printStackTrace();
          }
  
      }
  
  
      private static boolean compareToIntervales(Date filterFrom, Date filterTo, Date dateFrom, Date dateTo) {
          if ((dateFrom.equals(filterFrom) || dateFrom.after(filterFrom))
                  &amp;&amp; (dateTo.equals(filterTo) || dateTo.before(filterTo))) {
              return true;
  
          } else {
              return false;
          }
  
      }
  
  }
  </code></pre>