Write a Java program to print the current time in the different countries, In this java program example, we will see how to print the current time in the different countries.
Sample Input
Asia/Aden
Sample Output
current time is:09:03:19.232315600
Algorithm to print current time in the different countries
- Take the name of the country with the help of the ZoneIds method.
- Select the country.
- then print the local time according to zoneIds.
Java program to print current time in the different countries
import java.time.LocalTime; import java.time.ZoneId; public class Time { public static void main(String args []) { LocalTime t= LocalTime.now(ZoneId.of("Asia/Aden")); System.out.println("current time is :"+t); //available coutries in zoneId select(or copy) the countries from here and past on localtime object System.out.println("Available countries:"); for(String zone : ZoneId.getAvailableZoneIds()) { System.out.println(zone); } } }
Explanation of this Java program
Step 1: Start.
Step 2: Create the Time class and the static main method.
Step 3: Call the LocalTIme and ZoneId.getAvailableZoneIds() methods, Localtime to print the time and ZoneId to take input of countries.
Step 4: Select the country from zoneId and enter in LocalTime object.
Step 5: Print the current time of that country.
Step 6: End.
Output
current time is:09:03:19.232315600
#Available countries :
Asia/Aden
America/Cuiaba
Etc/GMT+9
Etc/GMT+8
Africa/Nairobi
America/Marigot
Asia/Aqtau…..
…………………….
……………………
US/Pacific
Europe/Monaco
In this way, we learned how to print the current time of different countries.