Java Program to display Current Time in another Time Zone


To display the current time in another time zone, use the TimeZone class. To use it, import the following package.

import java.util.TimeZone;

Firstly, set the TimeZone.

cal.setTimeZone(TimeZone.getTimeZone("Europe/Sofia"));

Now, display the date using the Calendar object.

cal.get(Calendar.HOUR_OF_DAY)
cal.get(Calendar.MINUTE)
cal.get(Calendar.SECOND)
cal.get(Calendar.MILLISECOND)

The following is the final example.

Example

 Live Demo

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));
   }
}

Output

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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 27-Jun-2020

701 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements