Take the 2-minute tour ×

Turns out this seems to be a complex subject, please tell me if my approach is too simple.

Basically what i'm trying to do is send an email reminder to a list of users (only when they have a reminder coming up, but that is besides the point) at 7 am every morning. Of course 7 am is at a different time based on the server location for everyone all over the world.

The plan is to collect to GMT offset hour via javascript from the user when they register:

var today = new Date();  
var offset = -(today.getTimezoneOffset()/60);

Simple enough, I now know the offset hour and store that in a database along with the rest of the users data. Now server side I have a TimerTask set up that runs every hour. I collect the system hour (say its 5 am), see how far away from my target time that is (in this case 2 (7-5)), get the system offset hour (in this case its -5), then pull all users from the database that have a GMT offset hour of -3 (-5 + 2), then do the email sending etc. At this point im assuming I know its 7 am for everyone whos time zone offset is -3 and I can continue with the process. This will run again the next hour and collect all users with a time zone offset of -4. Is this something that will actually work, or am I missing something?

Calendar systemTime = Calendar.getInstance();
int targetHour = 7;
int currentHour = systemTime.get(Calendar.HOUR_OF_DAY); //Its 5 am.
int difference = targetHour - currentHour; //2
int zoneOffset = systemTime.get(Calendar.ZONE_OFFSET) / (1000*60*60); //-5
int targetZone = zoneOffset + difference; //-3

List<User> users = userDAO.findByZoneOffset(targetZone);
for(User user : users) {
    //Send email or whatever I want to do with these users.

I understand its possible that the client time zone may be set incorrectly and therefore product an off result, but im willing to take that risk as its not absolutely necessary for the reminders to go out at exactly 7 am, just preferable.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted


That won't be enough to get a time zone consistently. It doesn't take account of daylight saving time. So if the user is anywhere that observes daylight saving time, they'll either get emails at 6am for about half the year, or they'll get emails at 8am for about half the year. You may well be better off guessing their time zone based on IP geolocation + offset.

I have a TimerTask set up that runs every hour.

I would suggest running more often than that. Otherwise, you may well find that you end up waking up just before you would send an email - so you then go back to sleep, and send the email at 7:59 next time... which may be 8:59 with your current scheme, based on client DST changes. At that point you're nearly two hours off your intended send time!

I collect the system hour

Don't do that, either. At this point there are two time zones to take account of - and your server time zone is completely irrelevant. Use UTC until you convert into the client's local time. It's going to make life much simpler. If you're still using Calendar, you can do this simply by setting its time zone.

Looking at your code, it looks like you also could end up missing a bunch of users completely, if you're checking by exact match of hour. If you end up running once at (say) 3:59 and then once at 4:01, you'd miss anyone who had to be run at 4. I would suggest keeping track of when you last sent a mail to each user - or possibly when you last ran - and use that to make sure you always catch everyone.

Finally, I'd strongly advise that you use Joda Time. It's a much cleaner date/time API, which will help you to think about the right concepts to use at the right point in your code.

share|improve this answer
I'm a little confused about the whole GMT offset thing in regards to DST. For instance, my time zone in NC is -5, to my knowledge that doesn't change throughout the year does it? If I were to check in spring, would it actually be -4? I figured that by using to system clock it would automatically adjust for DST. Then again, parts of the world don't use DST at all so I guess when the server was in DST it would calculate the incorrect time...err..time zones. –  ryandlf Dec 10 '12 at 7:28
@ryandlf: I don't know about North Carolina specifically, but most of the US observes daylight saving time. Do you change your clock every spring and fall? If so, that's changing the UTC offset. Note that the system clock of wherever you happen to run the server may well change DST on different dates to the user. Heck, it could easily change in a different direction (as the southern hemisphere observes DST in the opposite direction, e.g. putting their clocks back an hour in ~March, and putting them forward an hour in ~October). Or other countries have very different DST transitions. –  Jon Skeet Dec 10 '12 at 7:31
Do you know of any resources to collect the time zone based on ip geo? I'm having trouble finding any examples. –  ryandlf Dec 10 '12 at 7:33
@ryandlf: I suggest you search for "detecting time zone javascript" or something similar. Basically it's a fairly complicated business. Of course, one option is to ask the user. Make a guess, but allow the user to change their settings. –  Jon Skeet Dec 10 '12 at 7:35
add comment

Your Answer


