Java - 如何获得独立于系统日期的当前日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2891645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How to get current date independent from system date?
提问by Yatendra Goel
I want to know the current Date and Time.
我想知道当前的日期和时间。
The code
编码
Calendar.getInstance();
represents a date and time of the system on which the program is running and the system date can be wrong.
表示运行程序的系统的日期和时间,系统日期可能是错误的。
So Is there any way by which I can get correct current date and time irrespective of the date and time of the system on which program is running?
那么有什么方法可以让我获得正确的当前日期和时间,而不管运行程序的系统的日期和时间如何?
采纳答案by Paul Tomblin
If you're on the internet, you might be able to ask a known and trusted time source. If the person running your program wants to prevent your program from doing that (like if you've given them a time limited license and they don't want to pay for more time), they might spoof or block that connection.
如果您在 Internet 上,您或许可以询问已知且可信的时间来源。如果运行你的程序的人想要阻止你的程序这样做(比如如果你给了他们一个有时间限制的许可并且他们不想支付更多的时间),他们可能会欺骗或阻止该连接。
On one project I was on, we placed a secure, trusted time source in the hardware that could not be tampered with. It was designed for encryption and licensing, and had a Java library to access it. Sorry, I can't remember the name of the device.
在我参与的一个项目中,我们在硬件中放置了一个无法篡改的安全、可信的时间源。它是为加密和许可而设计的,并有一个 Java 库来访问它。抱歉,我不记得设备的名称。
So the answer is maybe yes, maybe no.
所以答案可能是肯定的,也可能不是。
回答by Mr.Expert
The programming level methods are developed to Get Date and Time from your system itself. You cannot modify them to get the dates except than the System specified.
编程级别的方法被开发为从您的系统本身获取日期和时间。除了系统指定的日期之外,您无法修改它们以获取日期。
For your additional requirement, if you wish to really have it, you would require a Synchronization between your Client Machine and Server.
对于您的额外要求,如果您真的希望拥有它,您需要在您的客户端机器和服务器之间进行同步。
回答by Dewfy
Have your system Internet access? If so, you can use synchronization with precise time services (for example: http://tldp.org/HOWTO/TimePrecision-HOWTO/ntp.html) and grant that you want.
您的系统可以访问 Internet 吗?如果是这样,您可以使用与精确时间服务(例如:http: //tldp.org/HOWTO/TimePrecision-HOWTO/ntp.html)的同步并授予您想要的。
回答by Martijn Courteaux
I don't understand completely your question but I can answer your title:
我不完全理解你的问题,但我可以回答你的标题:
GregorianCalendar gc = new GregorianCalendar(System.getCurrentTimeMillis());
int year = gc.get(Calendar.YEAR);
回答by theJollySin
In versions of Java prior to 1.1 it was standard to use the Dateclass:
在 1.1 之前的 Java 版本中,使用Date类是标准的:
Date now = new Date(); // Gets the current date and time
int year = now.getYear(); // Returns the # of years since 1900
However, in newer versions of Java, much of the Date
class has been deprecated (specifically the getYear
method). It is now more standard to use the Calendarclass:
但是,在较新版本的 Java 中,Date
该类的大部分内容已被弃用(特别是getYear
方法)。现在使用Calendar类更加标准:
Calendar now = Calendar.getInstance(); // Gets the current date and time
int year = now.get(Calendar.YEAR); // The current year
回答by Cosimo
Here's some code to fetch the date in HTTP format (usually UTC timezone) from a web server of your choice.
下面是一些代码,用于从您选择的 Web 服务器获取 HTTP 格式(通常是 UTC 时区)的日期。
Of course, if you have no control over the physical hardware and OS, nothing can guarantee you will be able to talk to the actual web server you ask for... but anyway.
当然,如果您无法控制物理硬件和操作系统,则无法保证您能够与您要求的实际 Web 服务器进行通信……但无论如何。
package some.package;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class Test {
private static String getServerHttpDate(String serverUrl) throws IOException {
URL url = new URL(serverUrl);
URLConnection connection = url.openConnection();
Map<String, List<String>> httpHeaders = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : httpHeaders.entrySet()) {
String headerName = entry.getKey();
if (headerName != null && headerName.equalsIgnoreCase("date")) {
return entry.getValue().get(0);
}
}
return null;
}
public static void main(String[] args) throws IOException {
String serverUrl = args.length > 0 ? args[0] : "https://google.com";
System.out.println(getServerHttpDate(serverUrl));
}
}