Java Program To Get The Current Time In Different Time Zones

Chapter: Date and Time Last Updated: 22-06-2023 14:20:01 UTC

Program:

            /* ............... START ............... */
                

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

public class TimeZoneExample {
    public static void main(String[] args) {
        // Define the time zones you want to retrieve the current time for
        String[] timeZones = {"America/New_York", "Europe/London", "Asia/Tokyo"};

        // Get the current time for each time zone
        for (String timeZone : timeZones) {
            LocalDateTime currentTime = LocalDateTime.now(ZoneId.of(timeZone));
            String formattedTime = currentTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

            System.out.println("Current time in " + timeZone + ": " + formattedTime);
        }
    }
}

                /* ............... END ............... */
        

Output

Current time in America/New_York: 2023-06-22 09:32:15
Current time in Europe/London: 2023-06-22 14:32:15
Current time in Asia/Tokyo: 2023-06-22 22:32:15


Please note that the actual output may vary depending on the current time when you run the program. 
The example output above assumes that the current time is 09:32:15 in New York, 14:32:15 in London, 
and 22:32:15 in Tokyo.

Notes:

  • In this example, we use the java.time.LocalDateTime class along with java.time.ZoneId and java.time.format.DateTimeFormatter to get the current time in different time zones.
  • You can customize the timeZones array to include the specific time zones you're interested in. The program will iterate over each time zone and retrieve the current time using LocalDateTime.now(ZoneId.of(timeZone)). The resulting time is then formatted using the DateTimeFormatter and displayed on the console.
  • Please note that the program relies on the Java 8 date and time API (java.time package) introduced in Java 8. If you're using an older version of Java, you may need to use a third-party library like Joda-Time to achieve the same functionality.

Tags

Java program to get the current time in different time zones # Get current time in specific timezone java # How to get timezone from date in Java

Similar Programs Chapter Last Updated
Java Program to Calculate Time Difference In Hours Date and Time 20-09-2023
Java Code To Calculate Time Difference In Milliseconds Date and Time 20-09-2023
Java Program To Calculate Time Difference In Seconds Date and Time 20-09-2023
How to calculate time difference between two time zones in Java Date and Time 20-09-2023
Java Date And Time API To Create A Custom Calendar Date and Time 20-09-2023
How To Format Date And Time In Java Date and Time 16-09-2023
How Do You Get The First Day Of The Week In Java Date and Time 15-09-2023
Java Code To Change Date Format Date and Time 15-08-2023
Java Program To Find Day Of The Week For A Given Date Date and Time 10-08-2023
Java code to check if date is weekend Date and Time 29-06-2023
Java program to find number of days between two dates Date and Time 29-06-2023
How To Add Working Days To A Date In Java Date and Time 22-06-2023
Java Program To Add A Specified Number Of Days To A Given Date Date and Time 22-06-2023
Write A Program To Check Leap Year In Java Date and Time 22-06-2023
Write A Program To Display Current Date And Time In Java Date and Time 22-06-2023
Java Date Format AM PM Date and Time 14-05-2023
Java Add Months To Date Date and Time 14-05-2023
Add Days To Date In Java example Date and Time 14-05-2023
Java Program To Calculate Days Between Two Dates Date and Time 16-04-2023
Java Program To Add Days To Date Date and Time 16-04-2023
How To Find Difference Between Two Dates In Java Date and Time 01-04-2023
Add Days To Date Java Example Date and Time 08-08-2021
Time Difference Between Two Timestamps In Java Date and Time 22-09-2018

1