Java(逻辑错误)时区的当前时间:美国东部时间
我的代码中似乎有一个逻辑错误。现在的时间是:14:38,但是 我的密码是18:38。我知道我可以用日历课,但我想 知道为什么这个代码是错误的 代码如下:Java(逻辑错误)时区的当前时间:美国东部时间,java,time,Java,Time,我的代码中似乎有一个逻辑错误。现在的时间是:14:38,但是 我的密码是18:38。我知道我可以用日历课,但我想 知道为什么这个代码是错误的 代码如下: public class welcome{ public static void main(String args[]){ //get total milliseconds since 1970 long total_millisec = System.currentTimeMillis(); // compute
public class welcome{
public static void main(String args[]){
//get total milliseconds since 1970
long total_millisec = System.currentTimeMillis();
// compute total seconds since 1970
long total_sec = total_millisec / 1000;
//compute current second
long current_sec = total_sec % 60;
//compute total minutes since epoch
long total_mins = total_sec / 60;
//compute current minute
long current_min = total_mins % 60;
//compute total hours
long total_hours = total_mins / 60;
//compute current hour
long current_hour = total_hours % 24;
System.out.println("Time is: "+current_hour+":"+current_min+":"
+current_sec);
}
}
执行计算时,假定System.currentTimeMillis()返回1970年1月1日午夜(即1970-01-01 00:00)和当前时间之间的差值(毫秒)。尝试评估系统中的基准日期,并查看它将是什么:
System.out.println("" + new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm").format(new java.util.Date(0)));
它可能会返回1969-12-3119:00,而现在不是午夜
System.currentTimeMillis()返回与表达式相同的值:
long currentTime = new java.util.Date().getTime() - new java.util.Date(0).getTime();
你在哪个时区
System.currentTimeMillis()
返回UTC 1970年1月1日以来的毫秒数。我在美国。东部时间尼斯!因此,需要进行更多的计算才能将其发送到ETAs f1sh,已经注释过,System.currentTimeMillis()
为您提供了UTC的值偏移量(EST为UTC-5,EDT为UTC-4)。请注意,在这里的日期格式化程序中使用了Java的区域设置:您将使用您的区域设置来告诉其他Java代码您正在进行EST。