The java.util.Date class is a container that contains several milliseconds from January 1, 1970, 00:00:00 UTC. Note that the Date class is not aware of the time zone. Use the Calendar class if you need to work with time zones. (edit 19-Jan-2017: if you are using Java 8, use the new date and time API in the java.time package).
The Date class is not suitable for storing an hour number (e.g. 13:00 or 18:00) without a date. It just is not done for this purpose, so if you try to use it the way you seem to be doing, you will encounter a number of problems and your solution will not be elegant.
If you forgot to use the Date class to store working hours and just use integers, this will be much simpler:
Date userDate = ...; TimeZone userTimeZone = ...; int companyWorkStartHour = 13; int companyWorkEndHour = 18; Calendar cal = Calendar.getInstance(); cal.setTime(userDate); cal.setTimeZone(userTimeZone); int hour = cal.get(Calendar.HOUR_OF_DAY); boolean withinCompanyHours = (hour >= companyWorkStartHour && hour < companyWorkEndHour);
If you also want to take into account minutes (not just hours), you can do something like this:
int companyWorkStart = 1300; int companyWorkEnd = 1830; int time = cal.get(Calendar.HOUR_OF_DAY) * 100 + cal.get(Calendar.MINUTE); boolean withinCompanyHours = (time >= companyWorkStart && time < companyWorkEnd);
Jesper
source share