Let's face it: it's a global economy.

That means your Java application could have users from all over the world.

Unfortunately, all those users don't live in the same time zone. So you might need to convert an object that represents a time in one time zone to an object that represents that same point in time in a different time zone.

For example: Greenwich Mean Time (GMT) is four hours ahead of the the current time on the east coast of the United States. (Thank you, Daylight Sacings Time).

So there might come a time when I need to convert a time from GMT to EDT. 

Thankfully, the Java programming language makes it easy to do that.

Since Java 8, anyway.

Start With LocalDateTime

Start with a LocalDateTime object. You can instantiate that fairly easily:

LocalDateTime localDateTime = LocalDateTime.now();

Of course, that object lives up to its name. It's the local date and time.

No time zone associated with it.

So let's give it a time zone.

Zoning In

Now convert that bad boy to a ZonedDateTime object:

ZonedDateTime zonedGmt = localDateTime.atZone(ZoneId.of("GMT"));

Once again, the object lives up to its name. It's converting the LocalDateTime object to a ZonedDateTime object which means it represents a point in time in a specific time zone.

But which time zone? I decided to go with GMT. That's why you see ZoneId.of("GMT") in the code above.

Incidentally, you can get a list of all possible ZoneId region names with the following code:

System.err.println(ZoneId.getAvailableZoneIds());

So at this point you've got a local date and time in a specific time zone.

Now how do you convert that to a different time zone?

Get It in an Instant

Here's how you convert that to a different time zone:

ZonedDateTime anotherZone = zonedGmt.withZoneSameInstant(ZoneId.of("US/Eastern"));

You do it with the withZoneSameInstant() method.

Yeah. Weird name.

That method accepts a single parameter: a ZoneId object.

So use the ZoneId.of() thing again to get the target time zone. In the example above, I'm aiming for the US/Eastern time zone.

Here's the whole block of code:

LocalDateTime localDateTime = LocalDateTime.now();
System.err.println(localDateTime);

ZonedDateTime zonedGmt = localDateTime.atZone(ZoneId.of("GMT"));
System.err.println(zonedGmt);

ZonedDateTime anotherZone = zonedGmt.withZoneSameInstant(ZoneId.of("US/Eastern"));
System.err.println(anotherZone);

And here's what that prints out if you run it:

2021-04-20T18:16:10.051715800
2021-04-20T18:16:10.051715800Z[GMT]
2021-04-20T14:16:10.051715800-04:00[US/Eastern]

The first line in the output is the LocalDateTime object. Again: no time zone.

The second line is the first ZonedDateTime object in the GMT time zone.

And the third line is the second ZonedDateTime object that represents the same tine on the east coast of the United States.

Wrapping It Up

Congratulations! You've converted a Java ZonedDateTime object from one time zone to another.

Now feel free to use that kind of code in your own applications.

Have fun!

Photo by cottonbro from Pexels