我需要创建一个程序,可以找到当前的GMT时间,并将其转换为EST时间。
当我尝试编译和运行程序时,我收到了这个错误:currentHourEST无法解析为变量
。我认为我的问题可能出现在if else
语句中,因为我可能分配变量错误或者其他原因。
// 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);
我正在使用if else语句将
offSetAdded
乘以-1
,这样如果小时数是负数,当我从中减去5
时,它就变成正数,使人们更容易看到小时数。如果offSetAdded
是正数,则会打印刚刚减去5的currentHour
。