Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS


1. Overview

In this quick tutorial, we'll see how to set the time zone of a date using Java 7, Java 8 and the Joda-Time library.

2. Using Java 8

Java 8 introduced a new Date-Time API for working with dates and times which was largely based off of the Joda-Time library.

The Instant class from Java Date Time API models a single instantaneous point on the timeline in UTC. This represents the count of nanoseconds since the epoch of the first moment of 1970 UTC.

First, we'll obtain the current Instant from the system clock and ZoneId for a time zone name:

Instant nowUtc = Instant.now();
ZoneId asiaSingapore = ZoneId.of("Asia/Singapore");

Finally, the ZoneId and Instant can be utilized to create a date-time object with time-zone details. The ZonedDateTime class represents a date-time with a time-zone in the ISO-8601 calendar system:

ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);

We've used Java 8's ZonedDateTime to represent a date-time with a time zone.

3. Using Java 7

In Java 7, setting the time-zone is a bit tricky. The Date class (which represents a specific instant in time) doesn't contain any time zone information.

First, let's get the current UTC date and a TimeZone object:

Date nowUtc = new Date();
TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone);

In Java 7, we need to use the Calendar class to represent a date with a time zone.

Finally, we can create a nowUtc Calendar with the asiaSingapore TimeZone and set the time:

Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore);

It's recommended to avoid the Java 7 date time API in favor of Java 8 date time API or the Joda-Time library.

4. Using Joda-Time

If Java 8 isn’t an option, we can still get the same kind of result from Joda-Time, a de-facto standard for date-time operations in the pre-Java 8 world.

First, we need to add the Joda-Time dependency to pom.xml:


To represent an exact point on the timeline we can use Instant from org.joda.time package. Internally, the class holds one piece of data, the instant as milliseconds from the Java epoch of 1970-01-01T00:00:00Z:

Instant nowUtc = Instant.now();

We'll use DateTimeZone to represent a time-zone (for the specified time zone id):

DateTimeZone asiaSingapore = DateTimeZone.forID("Asia/Singapore");

Now the nowUtc time will be converted to a DateTime object using the time zone information:

DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore);

This is how Joda-time API can be used to combine date and time zone information.

5. Conclusion

In this article, we found out how to set the time zone in Java using Java 7, 8 and Joda-Time API. To learn more about Java 8’s date-time support check out our Java 8 date-time intro.

As always the code snippet is available in the GitHub repository.


November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!