java Calendar, Date, and Time management for a multi-timezone application

In general, scheduling future events is a complex subject. You have to make a distinction in the context of what is going to be scheduled:

  • Does the event occur at a specific universal instant in time? If so, you should record the event time in terms of UTC.

    For example, a task that runs every 24 hours would be scheduled by UTC time and not by the local time. It might start off at some local midnight, but as daylight saving time changes take effect it could be running at 23:00 or 01:00 by the local clock.

  • However, if the event is scheduled by human beings, it is likely to be in terms of a local time, so you should record it that way.

    For example, a meeting that occurs at 08:00 Eastern Time will always occur at that local time. In the winter, that would be 13:00 UTC, and in the summer it would be at 12:00 UTC.

    So in this context, you cannot record the scheduled start time in terms of UTC. This is a very common mistake, as there is a ton of advice on the Internet that says “always store using UTC”, which would be wrong in this scenario.

    Instead, you should store two values – the local time such as 08:00 and its IANA time zone identifier, such as America/New_York. You may also need to store a recurrence pattern or specific date depending on how the event is scheduled.

  • Consider using Joda Time instead of Java’s Calendar or Date classess. It will save you from many headaches. Make sure you read the Joda Time documentation and understand how it works.

    Joda Time has all of the functions you will need for converting between one time zone and another – which I believe was the primary concern of your question.

  • Be sure to have a procedure in place for updating the time zone data regularly. Updates are pushed out multiple times a year, as the governments of the world make changes to the legal definitions of their time zones. You cannot just deploy it once and forget about it.

  • Also be sure you understand that conversion from local time to a specific UTC moment is not a perfect function due to daylight saving time. If an event is scheduled during an invalid or ambiguous local time, you should have a strategy for detecting and dealing with that in your application. You might just apply some assumptions, or you might want to go out of your way to ask the user what to do.

    For example, if I schedule an event at 2:00 AM Eastern Time every day, then on March 10th 2013, that time does not exist. Should the event occur at 3:00 AM? Or should it not occur at all?

    Another example, if I schedule an event at 1:00 AM Eastern Time every day, then on November 3rd, 2013, that time occurs twice. Should the event happen at the first (daylight time) instance? Or at the second (standard time) instance? Or both? Should I assume one or the other, or should I ask the user which they mean?

    Only you can decide what to do, as it is your application. But ignoring the problem will likely lead to errors.

  • Once an event has passed, you can record it in UTC if you wish, or record it with the full local date time and offset. Either are acceptable. This works just fine for singular past events, just not for recurring future ones.

Leave a Comment