java if 定义变量_java – 如何分配在if else语句中定义的变量

我需要创建能够在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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值