I have 2 date object in the database that represent the company’s working hours.
I only need the hours but since I have to save date. it appears like this:
Date companyWorkStartHour;
Date companyWorkEndHour;
start hours: 12-12-2001-13:00:00
finish hours: 12-12-2001-18:00:00
I have the timezone of the company and of the user. (my server may be in another timezone).
TimeZone userTimeZone;
TimeZone companyTimeZone;
I need to check if the user’s current time (considering his timezone) is within the company working hours (considering the company’s time zone).
How can I do it? I am struggling for over a week with Java calendar and with no success!
The
java.util.Dateclass is a container that holds a number of milliseconds since 1 January 1970, 00:00:00 UTC. Note that classDatedoesn’t know anyting about timezones. Use classCalendarif you need to work with timezones. (edit 19-Jan-2017: if you are using Java 8, use the new date and time API in packagejava.time).Class
Dateis not really suited for holding an hour number (for example 13:00 or 18:00) without a date. It’s simply not made for that purpose, so if you try to use it like that, as you seem to be doing, you’ll run into a number of problems and your solution won’t be elegant.If you forget about using class
Dateto store the working hours and just use integers, this will be much simpler:If you also want to take minutes (not just hours) into account, you could do something like this: