Universal Time Converter

A solar flare is expected to hit the earth on March 8th at 0625 UT so I decided to quickly make a time converter in Java.  The code doesn’t do much besides basic addition and subtraction.

More information on current weather events in space

http://www.spaceweather.com/

More information on time zones and universal time (UT)

http://eclipse.gsfc.nasa.gov/SEhelp/TimeZone.html

This source code can also be found at http://ideone.com/GbK1G.

import java.util.*;
import java.lang.*;

//Universal Time Converter
class UT_Converter{

        private int hr;
        private int min;
        private int sec;
        private String[] timeZones;

        public UT_Converter(int hr, int min ){

           // Checking the time
                if(hr >= 0 && hr = 0 && min                         this.min = min;
                else
                        this.min = -1; //Error!

           // Populating the timeZones array
                this.populateZones();
        }

        private void populateZones(){
                this.timeZones = new String[5];
                this.timeZones[0] = "ATLANTIC";
                this.timeZones[1] = "EASTERN";
                this.timeZones[2] = "CENTRAL";
                this.timeZones[3] = "MOUNTAIN";
                this.timeZones[4] = "PACIFIC";
        }

        public int toTimeZone(String timeZone, Boolean daylightSavings){
                timeZone = timeZone.toUpperCase();
                int time = -1;

                // ATLANTIC
                if( timeZone == this.timeZones[0] )
                        time = this.hr - 4;

                // EASTERN
                else if(timeZone == this.timeZones[1] )
                        time = this.hr - 5;

                // CENTRAL
                else if(timeZone == this.timeZones[2] )
                        time = this.hr - 6;

                // MOUNTAIN
                else if(timeZone == this.timeZones[3] )
                        time = this.hr - 7;

                // PACIFIC
                else if(timeZone == this.timeZones[4] )
                        time = this.hr - 8;

                if(daylightSavings)
                        time += 1;

           // Special case when we have to roll back the clock.
                if( time  < 0 )
                        time += 24;

                return(time);
        }

        public void toString(int hour){
                System.out.println(hour + ":" + this.min);
        }

        public static void main (String[] args) throws java.lang.Exception{
                //Sample Code
                UT_Converter UT = new UT_Converter(2,25);
                UT.toString(UT.toTimeZone("EASTERN", true));

        }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s