Struggling with how to compare clocks with different time zones in Java?

I have 2 date objects in the database that represent company time.

I need a watch, but I have to save the date. it looks like this:

Date companyWorkStartHour; Date companyWorkEndHour; 

opening hours: 12-12-2001-13: 00: 00 End time: 12-12-2001-18: 00: 00

I have the time zone of the company and user. (my server may be in a different time zone).

 TimeZone userTimeZone; TimeZone companyTimeZone; 

I need to check whether the current time of the user (given his time zone) is within the working time of the company (given the time zone of the company).

How can i do this? I have been scared for more than a week with the Java calendar and without success!

+7
java date timezone calendar
source share
4 answers

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); 
+6
source share

Try something like this:

 Calendar companyWorkStart = new GregorianCalendar(companyTimeZone); companyWorkStart.setTime(companyWorkStartHour); Calendar companyWorkEnd = new GregorianCalendar(companyTimeZone); companyWorkEnd.setTime(companyWorkEndHour); Calendar user = new GregorianCalendar(userTimeZone); user.setTime(userTime); if(user.compareTo(companyWorkStart)>=0 && user.compareTo(companyWorkEnd)<=0) { ... } 
+2
source share

Hey, I'm not sure how you do this using the Java calendar, but I highly recommend using the Joda Time package. It is a much simpler system to use, and it gives you direct methods for extracting all subcomponents of data and time, and even for creating simple time objects without considering the date. Then I assume that it will be a matter of comparing the two differences in the time zone and subtracting the difference from the JodaTime object.

0
source share

I have not tried the Jody library. This code should work.

 public boolean checkUserTimeZoneOverLaps(TimeZone companyTimeZone, TimeZone userTimeZone, Date companyWorkStartHour, Date companyWorkEndHour, Date userCurrentDate) { Calendar userCurrentTime = Calendar.getInstance(userTimeZone); userCurrentTime.setTime(userCurrentDate); int year = userCurrentTime.get(Calendar.YEAR); int month = userCurrentTime.get(Calendar.MONTH); int day = userCurrentTime.get(Calendar.DAY_OF_MONTH); Calendar startTime = Calendar.getInstance(companyTimeZone); startTime.setTime(companyWorkStartHour); startTime.set(Calendar.YEAR, year); startTime.set(Calendar.MONTH, month); startTime.set(Calendar.DAY_OF_MONTH, day); Calendar endTime = Calendar.getInstance(companyTimeZone); endTime.setTime(companyWorkEndHour); endTime.set(Calendar.YEAR, year); endTime.set(Calendar.MONTH, month); endTime.set(Calendar.DAY_OF_MONTH, day); if (userCurrentTime.after(startTime) && userCurrentTime.before(endTime)) { return true; } return false; } 

EDIT Updated code to reflect Bruno's comments. You should not take the terms of the company.

0
source share

All Articles