Determine if an hour is between an interval in Java

Description

The following code shows how to determine if an hour is between an interval.

Example


//ww w.j a  va2 s .  c  om
import java.text.SimpleDateFormat;
 
public class Main{
    static String  HOUR_FORMAT = "HH:mm";
    static SimpleDateFormat sdfHour = new SimpleDateFormat(HOUR_FORMAT);
    public static boolean isHourInInterval(String target, String start, String end) {
        return ((target.compareTo(start) >= 0)&& (target.compareTo(end) <= 0));
    }
    public static void main (String[] args) {
      String now = "12";
      String start = "14:00";
      String end   = "14:26";
      System. out.println(now + " between " + start + "-" + end + "?");
      System. out.println(isHourInInterval(now,start,end));
    }
   
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone