Friday, July 20, 2012

WORLD Clock in JAVA


World Clock Code in Java:


Input Name of City across world and Print the Current Time.


1. It takes name of city from user.
2. Then it Parses name and Time Zones which is being returned by for loop.(note that for loop below is returning names 455 Time zones across world, with no duplicity at all!!! )
3. As soon your input and Time Zones matches if-block takes current timezone in loop and passes it to Calendar.



import java.util.*;
public class UltimateWorldClock {
    private static final String TAG="ULTIMATE WORLD CLOCK is Product of Alexander & BROS. CORP";
  private static final String GLOBAL_LIST =
      "^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*";
  private static List<TimeZone> TimeZoneS;
  public static List<TimeZone> getTimeZones() {
    if (TimeZoneS == null) {
      TimeZoneS = new ArrayList<TimeZone>();
      final String[] tZoneID = TimeZone.getAvailableIDs();
      for (final String id : tZoneID) {
        if (id.matches(GLOBAL_LIST)) {
          TimeZoneS.add(TimeZone.getTimeZone(id));
        }
      }
      Collections.sort(TimeZoneS, new Comparator<TimeZone>() {
        public int compare(final TimeZone t1, final TimeZone t2) {
          return t1.getID().compareTo(t2.getID());
        }
      });
    }
    return TimeZoneS;
  }
  public static String getName(TimeZone timeZone)//This is for Displaying Timezone ID and TimeZone Name
  { 
    return timeZone.getID().replaceAll("_", " ");
  }
  public static void main(String[] args) {
    TimeZoneS = getTimeZones();
    Scanner in=new Scanner(System.in);
    System.out.println(TAG+"\nEnter the Name of City You wish to Know Time of:");
    String city=in.next();
   for (TimeZone timeZone : TimeZoneS) {
 //System.out.println(getName(timeZone));
 if(getName(timeZone).matches("(?i).*"+city+".*"))
 {
     Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(getName(timeZone)));
            int hour12 = cal.get(Calendar.HOUR);         
            int minutes = cal.get(Calendar.MINUTE);      
            int seconds = cal.get(Calendar.SECOND);      
            boolean am = cal.get(Calendar.AM_PM) == Calendar.AM;
            boolean pm = cal.get(Calendar.AM_PM) == Calendar.PM;
            if (am) {
                System.out.println(getName(timeZone) + " | Current Time: " + hour12 + ":" + minutes + ":" + seconds + " AM");
            }
            if (pm) {
                System.out.println(getName(timeZone) + " | Current Time: " + hour12 + ":" + minutes + ":" + seconds + " PM");
    }
 }
   }
  }
  }

You can try Example such as:
sofia or sof
sarajevo
tehran
kolkata
los angles
hong kong
........... 

No comments:

Post a Comment

Generate all possible combination of numbers in java

The Idea is fairly Simple. Break any number chosen in combination of four numbers. Concept of solution: If we are choosing any number from...