Writing timestamps to database with two servers in different time zones



The problem

I have two different applications accessing the same database.  One runs on a server in the Pacific Time Zone.  The other, newer app runs on a server using UTC.  The two applications were writing timestamps to the database with different time zones.  Those two times are either 7 or 8 hours difference, depending on whether we’re in Daylight Savings Time or Standard Time.

To make matters worse, comparisons needed to be made between dates written by the two different applications.  Clearly they needed to be using the same time zone.

The handling

Probably many ways to handle, and this may not even be the best way.  But it is a way that worked.

The newer app on the UTC server uses Springboot.

  • Set up the time zone ID as a parameter in the application properties YAML file.  The time zone ID to use in this case is “America/Los_Angeles”.  Looks like this:
zoneid: America/Los_Angeles
  • In the classes that need to set the timestamp, declare a string variable to hold the time zone ID.  Annotate it with @Value so it looks like this:
private String zoneId;
  • Add a method that runs when the class is first initialized, to establish the time zone.
void started() {

When the entity is written to the database, the timestamps will have the local time, in this case Pacific Time.

If I weren’t trying to keep backward compatibility with a legacy system, I would prefer writing the timestamps to the database using UTC time zones.  Then the displaying software can determine what local time zone to display them in.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s