Write a Java program to get the current time in all the available time zones


This program prints the current date and time in various time zones using the java.time package in Java. Here's how it works:

  • ZoneId.SHORT_IDS.keySet() returns a set of all the short time zone IDs available in the java.time package.
  • The forEach() method is called on the stream of short time zone IDs returned by the previous step.
  • For each short time zone ID, the program creates a ZoneId object using ZoneId.of() method by passing the short time zone ID.
  • The program then prints the current date and time in the time zone specified by the ZoneId object using the LocalDateTime.now() method.
  • The output of the program displays the short time zone ID followed by the current date and time in that time zone.

Source Code

import java.time.*;
class Current_Available_TimeZones
{
	public static void main(String[] args)
	{
		ZoneId.SHORT_IDS.keySet().
		stream().forEach(zoneKey ->System.out.println(" "+ ZoneId.of( ZoneId.SHORT_IDS.get( zoneKey ) ) +": "+ LocalDateTime.now(ZoneId.of(ZoneId.SHORT_IDS.get( zoneKey ) ) ) ) );
	}
}

Output

 Australia/Sydney: 2022-11-05T19:02:41.412034
 Asia/Kolkata: 2022-11-05T13:32:41.443283500
 America/Chicago: 2022-11-05T03:02:41.443283500
 Asia/Yerevan: 2022-11-05T12:02:41.443283500
 America/Argentina/Buenos_Aires: 2022-11-05T05:02:41.458907800
 Africa/Harare: 2022-11-05T10:02:41.458907800
 Pacific/Apia: 2022-11-05T22:02:41.458907800
 America/Sao_Paulo: 2022-11-05T05:02:41.458907800
 America/Los_Angeles: 2022-11-05T01:02:41.458907800
 Africa/Cairo: 2022-11-05T10:02:41.458907800
 Asia/Tokyo: 2022-11-05T17:02:41.458907800
 Asia/Ho_Chi_Minh: 2022-11-05T15:02:41.458907800
 America/Indiana/Indianapolis: 2022-11-05T04:02:41.458907800
 America/St_Johns: 2022-11-05T05:32:41.474532
 Asia/Karachi: 2022-11-05T13:02:41.474532
 America/Phoenix: 2022-11-05T01:02:41.474532
 -05:00: 2022-11-05T03:02:41.474532
 Africa/Addis_Ababa: 2022-11-05T11:02:41.474532
 Europe/Paris: 2022-11-05T09:02:41.474532
 America/Puerto_Rico: 2022-11-05T04:02:41.474532
 Asia/Shanghai: 2022-11-05T16:02:41.474532
 Pacific/Guadalcanal: 2022-11-05T19:02:41.474532
 -07:00: 2022-11-05T01:02:41.474532
 America/Anchorage: 2022-11-05T00:02:41.474532
 Pacific/Auckland: 2022-11-05T21:02:41.474532
 -10:00: 2022-11-04T22:02:41.474532
 Australia/Darwin: 2022-11-05T17:32:41.490157
 Asia/Dhaka: 2022-11-05T14:02:41.490157

Example Programs