fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. //Universal Time Converter
  5. class UT_Converter{
  6.  
  7. private int hr;
  8. private int min;
  9. private int sec;
  10. private String[] timeZones;
  11.  
  12. public UT_Converter(int hr, int min ){
  13.  
  14. // Checking the time
  15. if(hr >= 0 && hr <= 24)
  16. this.hr = hr;
  17. else
  18. this.hr = -1; //Error!
  19.  
  20. if(min >= 0 && min <= 60)
  21. this.min = min;
  22. else
  23. this.min = -1; //Error!
  24.  
  25. // Populating the timeZones array
  26. this.populateZones();
  27. }
  28.  
  29. private void populateZones(){
  30. this.timeZones = new String[5];
  31. this.timeZones[0] = "ATLANTIC";
  32. this.timeZones[1] = "EASTERN";
  33. this.timeZones[2] = "CENTRAL";
  34. this.timeZones[3] = "MOUNTAIN";
  35. this.timeZones[4] = "PACIFIC";
  36. }
  37.  
  38. public int toTimeZone(String timeZone, Boolean daylightSavings){
  39. timeZone = timeZone.toUpperCase();
  40. int time = -1;
  41.  
  42. // ATLANTIC
  43. if( timeZone == this.timeZones[0] )
  44. time = this.hr - 4;
  45.  
  46. // EASTERN
  47. else if(timeZone == this.timeZones[1] )
  48. time = this.hr - 5;
  49.  
  50. // CENTRAL
  51. else if(timeZone == this.timeZones[2] )
  52. time = this.hr - 6;
  53.  
  54. // MOUNTAIN
  55. else if(timeZone == this.timeZones[3] )
  56. time = this.hr - 7;
  57.  
  58. // PACIFIC
  59. else if(timeZone == this.timeZones[4] )
  60. time = this.hr - 8;
  61.  
  62. if(daylightSavings)
  63. time += 1;
  64.  
  65. // Special case when we have to roll back the clock.
  66. if( time < 0 )
  67. time += 24;
  68.  
  69. return(time);
  70. }
  71.  
  72. public void toString(int hour){
  73. System.out.println(hour + ":" + this.min);
  74. }
  75.  
  76. public static void main (String[] args) throws java.lang.Exception{
  77. //Sample Code
  78. UT_Converter UT = new UT_Converter(8,25);
  79. UT.toString(UT.toTimeZone("EASTERN", true));
  80.  
  81. }
  82. }
Success #stdin #stdout 0.08s 212416KB
stdin
Standard input is empty
stdout
4:25