Java Program to display Current Time in another Time Zone

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 17-Dec-2018 14:28:41
Advertisements