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!
Try something like this:
I haven't tried the Joda library. This code should work.
EDIT Updated the code to reflect Bruno's comments. Shouldn't be taking the dates of the company work timings.
Hey I am not sure how you would do this using the Java calendar but I would highly recommend using the Joda Time package. It's a much simpler system to use and it gives you direct methods to extracts all subcomponents of data and time and even just to create simple time objects without the date involved. Then I imagine it would be a matter of comparing the 2 timezone differences and subtracting the difference from the JodaTime object.
The
java.util.Date
class is a container that holds a number of milliseconds since 1 January 1970, 00:00:00 UTC. Note that classDate
doesn't know anyting about timezones. Use classCalendar
if 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
Date
is 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
Date
to 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: