[求助]时间奇怪的问题
写了个方法,是用来获取当前时间的。但不知道为什么有问题。。方法如下:
public static String getTime() {
long timer = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
System.out.println(sdf.format(timer));
}
输出的时间跟电脑上实际时间相差整8个小时,其它分秒不差。不知道什么问题。。。 long timer = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
System.out.println(sdf.format(timer));
}
搜索更多相关的解决方案:
时间
----------------解决方案--------------------------------------------------------
很明显是时区问题,一个是东八区,一个是格林威治标准时间。
----------------解决方案--------------------------------------------------------
???那我不是加了 Locale.CHINA?
----------------解决方案--------------------------------------------------------
Locale.CHINESE
----------------解决方案--------------------------------------------------------
结果是一样的 还是少8个小时
----------------解决方案--------------------------------------------------------
时区应该这样设置:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
class TestTime {
public static void getTime() {
long timer = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
System.out.println(sdf.format(timer));
}
}
class Start{
public static void main(String args[]){
TestTime.getTime();
}
}
----------------解决方案--------------------------------------------------------
SimpleDateFormat后面可以有第二个参数 就是设时区的呀
----------------解决方案--------------------------------------------------------
那样子我试过了,好像不行,可能不是设时区的。。。
----------------解决方案--------------------------------------------------------
是啊 就是不行啊 但是在以前我写的这个类运行的时候是好好的呀
然后最近我写一个项目的时候 发现在了这个问题 就怎么也搞不过来了
----------------解决方案--------------------------------------------------------