如果系统日期是当前日期的一年,如何在java中获取今天的日期?

问题描述:

我的动机是获得当前日期而不关心系统日期。

My motive is to get current date without caring of the system date.

我的建议,如果你真的不能依赖系统时间就是使用时间服务器。 Apache commons有一个非常有用的客户端,可以帮助解决这个问题。网上有很多例子:

My suggestion, if you really can't rely on the system time is to use a time server. Apache commons has a really useful client which will help with the this. There are plenty of examples online:

https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ntp/NTPUDPClient.html

还有许多可用的时间服务器。

There are also numerous available timeservers.

不要试图自己纠正日期代码,这将是脆弱的,如果有人纠正服务器时间将破坏。

Don't be tempted to just correct the date yourself in the code, this will be fragile and will break if someone corrects the server time.

显而易见的,更简单的解决方案是纠正服务器上的时间!

The obvious, and easier, solution is to correct the time on the server though!