Dealing with Timezone Differences in a Distributed Web Application

In a distributed web application, it is not uncommon to have different components running in different timezones. For example, a React frontend may run in one timezone, while a Java API runs in a different timezone, and the MySQL database hosted in a different timezone altogether. This can lead to confusion and errors when processing push and pull requests that involve timestamps and other time-related data.

To address this issue, it is important to establish a consistent timezone across all components. In this blog, we will explore how to achieve this using Coordinated Universal Time (UTC) as the standard timezone.

Why UTC?

UTC is a standard timezone that is not affected by daylight saving time or other regional variations. By using UTC as the standard timezone, we can ensure that all components are operating on the same time reference point. This eliminates the need for complex timezone calculations and prevents errors that can arise due to timezone differences.

In practice, this means converting all timestamps to UTC before storing them in the database or sending them to the API. When retrieving timestamps, we convert them back to the local timezone of the component that is processing the request. Let’s look at some examples to illustrate this approach.

Example: React Front-end

In the React frontend, we can use the 'moment-timezone' library to convert local times to UTC before sending them to the API:

import moment from ‘moment-timezone‘;

// Convert local time to UTC
const utcTime = moment(localTime).utc().format();

// Send request to API with UTC time‘/api/some-endpoint’, { time: utcTime });

Here, 'localTime' is the local time that we want to convert to UTC. We use the 'moment' library to create a moment object, which we then convert to UTC using the 'utc' method. Finally, we format the UTC time as a string and send it to the API.

Example: Java API

In the Java API, we can use the 'java.time' package to convert UTC times to the local timezone before processing the request:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

// Retrieve UTC time from request
String utcTime = request.getParameter(“time”);

// Convert UTC time to local timezone
Instant instant = Instant.parse(utcTime);
ZonedDateTime localTime = instant.atZone(ZoneId.systemDefault());

// Process request using local time

Here, 'utcTime' is the UTC time that we retrieved from the request. We use the 'Instant.parse' method to create an Instant object, which we then convert to the local timezone using the 'atZone' method. Finally, we use the local time to process the request.

Example: MySQL Database

In the MySQL database, we can store all times in UTC format by setting the database timezone to UTC:

SET time_zone = ‘+00:00’;

When retrieving times from the database, we can convert them to the local timezone using the 'CONVERT_TZ' function:

SELECT CONVERT_TZ(some_time, ‘+00:00’, @@session.time_zone) FROM some_table;

Here, 'some_time' is the UTC time that we want to convert to the local timezone. We use the 'CONVERT_TZ' function to convert the time to the local timezone, using the '@@session.time_zone' variable to get the current timezone of the session.

If timezone differences are not handled correctly in a distributed web application, it can lead to errors and inconsistencies in the data being processed. For example, if a timestamp is recorded in one timezone and then retrieved and processed in a different timezone without proper conversion, the resulting time may be incorrect and may cause issues such as:

Incorrect scheduling of events: If events are scheduled based on timestamps, incorrect timestamps due to timezone differences can result in events being scheduled at the wrong time or not at all.

Inaccurate reporting: If timestamps are used for reporting or analytics purposes, incorrect timestamps can lead to inaccurate reports and analytics.

Data inconsistencies: If different components of the application are processing data based on different timezones, it can lead to inconsistencies and errors in the data being stored and processed.

Confusion and frustration for users: If users are seeing incorrect times and dates, it can cause confusion and frustration and may lead to a poor user experience.

Therefore, it is important to handle timezone differences correctly in a distributed web application to ensure that the data being processed is consistent and accurate.


In a distributed web application, dealing with timezone differences can be a challenge. However, by establishing a consistent timezone across all components, we can ensure that timestamps and other time-related data are consistently understood and processed correctly