tags:

views:

44

answers:

1

Hi,

I have an application that passes in java.util.Date. I want to check whether this date is within a specified time of day (e.g. between 10:30 & 11:30), I don't care about the date, just the time of day.

Can anyone show me a simple way to do this?

Thanks

+2  A: 

This is what the Calendar class is for. Assuming date is your Date object:

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
if (hour == 10 && minutes >= 30 || hour == 11 && minutes <= 30) {
   ... 
}
cletus
Tip top, thank very much, must be having a slow day today
Gaz