Converting Date and Time between Timezones

Learn to convert a given date-time object from one timezone to another timezone. We will see the examples using ZonedDateTime, Date and Calendar classes.

1. Changing Timezones of ZonedDateTime

In Java 8, date and time with timezone information is represented with ZonedDateTime. To convert a ZonedDateTime instance from one timezone to another, follow the two steps:

  • Create ZonedDateTime in 1st timezone. You may already have it in your application.
  • Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.
ZonedDateTime instant = ZonedDateTime.now();
System.out.println(instant);

ZonedDateTime instantInUTC = instant.withZoneSameInstant(ZoneId.of("UTC"));
System.out.println(instantInUTC);

Program Output.

2022-02-16T18:36:14.509742500+05:30[Asia/Calcutta]
2022-02-16T13:06:14.509742500Z[UTC]

Read More: Convert Date to EST/EDT Timezone

2. Changing Timezones of OffsetDateTime

Similar to ZonedDateTime, OffsetDateTime also represents an instant in universal timeline with an offset from UTC/Greenwich in the ISO-8601 calendar system. To convert a OffsetDateTime instance from one timezone to another, follow the two steps:

  • Create OffsetDateTime in 1st timezone. You may already have it in your application.
  • Convert the first OffsetDateTime in second timezone using withOffsetSameInstant() method.
OffsetDateTime now = OffsetDateTime.now();
System.out.println(now);

OffsetDateTime nowInUTC = now.withOffsetSameInstant(ZoneOffset.of( "00:00" ));
System.out.println(instantInUTC);

Program Output.

2022-02-16T18:36:14.509742500+05:30
2022-02-16T13:06:14.509742500Z[UTC]

3. Changing Timezones of java.util.Date

  • java.util.Date represents a time instant without timezone information.
  • It only represents the total time since the epoch in milliseconds.
  • This is very important to understand that, by default, if we print the Date object, it will always print the date and time information along with the system’s current timezone. This is misleading behavior because it suggests that Date objects can have timezone information, which is incorrect.

The correct way to deal with Date instance in different timezones is to print the date information in other timezones using the SimpleDateFormat class. It is not a good solution to adjust the instant in the timeline by adjusting the zone offset in the milliseconds of Date object.

SimpleDateFormat FORMATTER = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mma z");

//In Default Timezone
Date currentDate = new Date();

//Date in current timezone
System.out.println(FORMATTER.format(currentDate));

//In UTC Timezone
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
FORMATTER.setTimeZone(utcTimeZone);
String sDateInUTC = FORMATTER.format(currentDate);
System.out.println(sDateInUTC);

Program Output.

02/16/2022 at 06:36pm IST
02/16/2022 at 01:06pm UTC

Happy Learning !!

Sourcecode on Github

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial