# Create if statement with hour and minute in range

Dear all,

I am wanting to create an if statement, i.e. if between 9.30 and 11.00 do something.

I'm getting the hour and minute values from the calendar object.

``````int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);
``````

Now that I'm using half hour data I'm unsure of how to update the if statement to check for hour and minute in range.

``````if ((hour >= 9) && (hour <= 11)) {
value = 1;
}
``````

Thanks
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You can use this:
``````if ((hour = 9 && min >= 30 || hour > 9) && (hour <= 11)) {
value = 1;
}
``````
0
Author Commented:
I have used your example,  for the hour 11 and minute 30  it is accessing the first  if statement when it shouldn't (only second). I made a change see below and it  still accesses both if statements.

``````Change to your example
if ((((hour == 9) && (minute >= 30)) || (hour > 9)) && (((hour == 11) && (minute == 0)) || (hour <= 11))) {
value = 1;
}

if ((((hour == 11) && (minute >= 30)) || (hour > 11)) && (hour <= 13)) {
value = 2;
}
``````
0
Commented:
You are right, it needs more restrictions, like this:
``````if ((hour == 9 && min >= 30 || hour > 9) && (hour < 11 || hour == 11 && min == 0)) {
value = 1;
}
``````
0

Experts Exchange Solution brought to you by