SimpleDateFormat返回1小时为什么



我有SimpleDateFormat.

Date vrcTime;
SimpleDateFormat vrcCurrentTimeTextValue;
vrcTime.setTime (0);
vrcTimeFormat = new SimpleDateFormat ("HH:mm:ss");
vrcCurrentTimeTextValue =  vrcTimeFormat.format (vrcTime);

为什么我的SimpleDateFormat返回1小时,即使时间是0 ?

您生活在GMT+1时区,除非您指定一个不同的时区,否则您的格式化程序将选择您当前的时区,因此它认为0小时为GMT,并且由于您在GMT+1,它输出1小时。所以添加

vrcTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

。将代码重写为

Date vrcTime;
SimpleDateFormat vrcCurrentTimeTextValue;
vrcTime.setTime (0);
vrcTimeFormat = new SimpleDateFormat ("HH:mm:ss");
vrcTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
vrcCurrentTimeTextValue =  vrcTimeFormat.format (vrcTime);

最新更新