vendredi 26 décembre 2014

How can you assign a variable that is defined inside an if else statement

I need to create something that will find the current hour in GMT and convert it into EST.


When I attempt to compile and run the program I receive this error: currentHourEST cannot be resolved to a variable. I think that my issue is someplace down in the if else statements being that I assigned the variables wrong or something.



// Obtain total milliseconds since midnight, Jan 1, 1970
long totalMilliseconds = System.currentTimeMillis();

// Seconds
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = totalSeconds % 60;

// Minutes
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;

// Hours
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24;

// Read in EST offset
long offSetAdded = currentHour - 5;

// If the time is negative make it a positive
if (offSetAdded > 0) {
long currentHourEST = offSetAdded * -1;
} else {
long currentHourEST = offSetAdded;
}

// Display time
System.out.println("The current time is " + currentHourEST + ":" + currentMinute + ":" + currentSecond);

System.out.println("Current time in GMT is " + currentHour + ":" + currentMinute + ":" + currentSecond);


I am using the if else statement to multiple the offSetAdded by -1 so that the hour, if it is negative when I subtract 5 from it, it becomes positive making it easier for people to see the hour. If the offSetAdded is positive, then it will print the currentHour just subtracted by 5.


Aucun commentaire:

Enregistrer un commentaire