简体   繁体   English

为什么date.gettime()在Java中总是返回上一个日期,而不是今天的日期?

[英]Why date.gettime() always returns Previous Date, not today's date in java?

I have following code, running on Unix server (WAS 5.1), it's a test environment in our company. 我有以下代码,在Unix服务器(WAS 5.1)上运行,它是我们公司的测试环境。 When I try to execute the below code, I always get yesterday's date (ie if I execute today 9/27/2012 it returns date as 9/26/2012) 当我尝试执行以下代码时,我总是得到昨天的日期(即如果我今天执行2012年9月27日,则返回日期为2012年9月26日)

long userSelectedTimeLong = date.getTime()

Date returns time based on TimeZone of the machine. 日期根据机器的时TimeZone返回时间。 It is possible that your sever might be hosted some where else and TimeZone value is different. 服务器可能托管在其他地方,并且TimeZone值不同。 You need to compare value of date by having same TimeZone. 您需要通过具有相同的TimeZone来比较日期的值。

Once you do that next task is is date variable really contains current date. 一旦执行该操作,下一个任务就是date变量确实包含当前日期。 You can print a simple log using new Date().getTime() 您可以使用new Date().getTime()打印简单的日志

我曾经遇到过类似的问题;-)检查是否在应用程序启动时实例化了本地日期变量,并且在应用程序运行时未刷新性能。

使用long userSelectedTimeLong = Calendar.getInstance().getTimeInMillis()会很好

Another possibility : does the server sync with some kind of time service ? 另一种可能性:服务器是否与某种时间服务同步? (ntp, ...) Maybe it 's just the system date on the Unix machine that has been set wrong. (ntp,...)也许只是Unix计算机上的系统日期设置有误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM