w3resource logo


Java Programming Exercies

Java DateTime, Calendar Exercises: Get the current time in all the available time zones

Secondary Nav

Java DateTime, Calendar : Exercise-21 with Solution

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

Java Code:

import java.time.*;
public class Exercise21 {
   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 ) ) ) ) );
    }
}

Sample Output :

 Asia/Shanghai: 2016-09-16T16:38:05.448                                 
 Africa/Cairo: 2016-09-16T10:38:05.450                                  
 America/St_Johns: 2016-09-16T06:08:05.451                              
 America/Puerto_Rico: 2016-09-16T04:38:05.453                           
 America/Phoenix: 2016-09-16T01:38:05.453                               
 Asia/Karachi: 2016-09-16T13:38:05.453                                  
 America/Anchorage: 2016-09-16T00:38:05.454                             
 Asia/Dhaka: 2016-09-16T14:38:05.454                                    
 ........
 America/Indiana/Indianapolis: 2016-09-16T04:38:05.458                  
 Pacific/Apia: 2016-09-16T21:38:05.458                                  
 Asia/Yerevan: 2016-09-16T12:38:05.458
 

Java Practice online:

import java.time.*;
public class Exercise21 {
   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 ) ) ) ) );
    }
}



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program