Display Current Time in Another Time Zone Using Java



A time zone is a region of the globe that observes a uniform standard time for legal, commercial, and social purposes. Suppose you have an application that is running in India as well as Japan. Here, you can't use the same time zone for both regions. Therefore, it is necessary to display time in different time zones.

Java provides various built-in classes like TimeZone and ZoneId and methods like getTimeZone() that can help in displaying the current time in another time zone. However, before using them, you must import them into your Java program.


Using getTimeZone() and setTimeZone() Methods

The TimeZone class of java.util package provides the method getTimeZone(), which accepts a time zone ID as a String and returns the time zone of the given ID. After fetching the time zone, set the time zone for Calendar object using its setTimeZone() method. Then, by calling get() method you can display the current time in the passed time zone ID.

Example

A Java program that demonstrates how to display current time in another time zone.

import java.util.Calendar;
import java.util.TimeZone; 
public class Demo {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      System.out.println("Europe/Sofia TimeZone...");
      cal.setTimeZone(TimeZone.getTimeZone("Europe/Sofia"));
      System.out.println("Hour = " + cal.get(Calendar.HOUR_OF_DAY));
      System.out.println("Minute = " + cal.get(Calendar.MINUTE));
      System.out.println("Second = " + cal.get(Calendar.SECOND));
      System.out.println("Millisecond = " + cal.get(Calendar.MILLISECOND));
   }
}

When you execute this code, it will show the following output ?

Europe/Sofia TimeZone...
Hour = 11
Minute = 16
Second = 44
Millisecond = 354

Using ZonedDateTime and ZoneId Classes

In this approach, we first retrieve the ZoneId object for the specified time zone ID using ZoneId.of() method. Then, using ZonedDateTime.now() create a ZonedDateTime object representing the current time of the retrieved ZoneId object. After these steps, you can use getHour(), getMinute(), and getSecond() methods to display the current time for the specified time zone.

Example

The following Java program displays the current time in another time zone using the ZonedDateTime and ZoneId classes.

import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class Demo {
   public static void main(String[] args) {
      System.out.println("America/New_York TimeZone...");
      ZonedDateTime current_time = ZonedDateTime.now(ZoneId.of("America/New_York"));
      System.out.println("Hour = " + current_time.getHour());
      System.out.println("Minute = " + current_time.getMinute());
      System.out.println("Second = " + current_time.getSecond());
   }
}

On running this code, you will get the below result ?

America/New_York TimeZone...
Hour = 8
Minute = 4
Second = 57

Using LocalTime and ZoneId Classes

This is another way of displaying current time in another time zone. Here, we use ZoneId.of() method to get the time zone and by passing this time zone to LocalTime.now() method as a parameter value, we retrieved the current time of that zone.

Example

In this Java program, we use the LocalTime and ZoneId classes to display current time in another time zone.

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class Demo {
   public static void main(String[] args) {
      System.out.println("Asia/Tokyo TimeZone...");
      LocalTime current_time = LocalTime.now(ZoneId.of("Asia/Tokyo"));
      System.out.println("Hour = " + current_time.getHour());
      System.out.println("Minute = " + current_time.getMinute());
      System.out.println("Second = " + current_time.getSecond());
   }
}

The above code will generate the following result ?

Asia/Tokyo TimeZone...
Hour = 21
Minute = 6
Second = 43
Updated on: 2024-09-30T15:54:09+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements