Java:从服务器而不是系统时钟获取当前日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2817475/
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: Get current Date and Time from Server not System clock
提问by Insectatorious
In my Java program, I need to create an instance of the current moment in time. I use
在我的 Java 程序中,我需要及时创建当前时刻的实例。我用
Date date = new Date();
This gives me the current date and time as per the host machine's system clock. Is there any way I can get the current date and time from an online server? The world time server perhaps?
这为我提供了主机系统时钟的当前日期和时间。有什么方法可以从在线服务器获取当前日期和时间?也许是世界时间服务器?
I have seen this postand it describes what I want but I'm afraid I need more help than what's provided there.
我看过这篇文章,它描述了我想要的东西,但恐怕我需要比那里提供的更多帮助。
In a nutshell, I want to get a date and time that is not dependant on the host machine's system clock.
简而言之,我想获得一个不依赖于主机系统时钟的日期和时间。
Thanks!
谢谢!
采纳答案by aioobe
You could have a look at the Java NTP Client demo available at
您可以查看 Java NTP Client 演示,网址为
http://www.docjar.com/html/api/examples/ntp/NTPClient.java.html
http://www.docjar.com/html/api/examples/ntp/NTPClient.java.html
and some example code that utilizes this client
以及一些使用此客户端的示例代码
http://www.docjar.com/html/api/examples/ntp/TimeClient.java.html
http://www.docjar.com/html/api/examples/ntp/TimeClient.java.html
It's about 170 lines of well documented java code.
它大约有 170 行记录良好的 Java 代码。
回答by ZeissS
Normally servers sync their time regulary with online time servers. So your server time should be accurate. If not, contact your admin.
通常服务器会定期与在线时间服务器同步时间。所以你的服务器时间应该是准确的。如果没有,请联系您的管理员。
To get e.g. the GMT time, use the Calendar and TimeZone classes.
要获得例如 GMT 时间,请使用 Calendar 和 TimeZone 类。
回答by Basil Bourque
Local network
本地网络
If your machine is connected to a local network, connect to some machine acting as a trustworthy time server. You could write your own such time server.
如果您的机器连接到本地网络,请连接到某些充当可信赖时间服务器的机器。您可以编写自己的此类时间服务器。
Network Time Protocol
网络时间协议
Or you could use the well-worn Network Time Protocol (NTP)with existing server and client implementations bundled with most any OS. For more on this, see the accepted Answer by aioobe.
或者,您可以将陈旧的网络时间协议 (NTP)与与大多数任何操作系统捆绑在一起的现有服务器和客户端实现一起使用。有关更多信息,请参阅aioobe 已接受的答案。
Consider placing that time server computer in network's DMZso as to updates from time servers on the Internet such as the pool.ntp.org projector those provided by the United States federal government (NIST).
考虑将该时间服务器计算机放置在网络的 DMZ 中,以便从 Internet 上的时间服务器(例如pool.ntp.org 项目或美国联邦政府 (NIST) 提供的项目)进行更新。
Internet
互联网
If your machine is connected to the internets, connect to a trustworthy time server. As discussed above, use a custom protocol such as a web service, or use NTP.
如果您的机器已连接到 Internet,请连接到值得信赖的时间服务器。如上所述,使用自定义协议(例如 Web 服务)或使用NTP。
This is discussed in the Question, how to make my java app get global time from some online clock.
这在问题中讨论,如何使我的 java 应用程序从某个在线时钟获取全球时间。
See also Questions: Java NTP client, and Java client for time server, such as Network Time Protocol (NTP).
另请参阅问题:Java NTP 客户端和时间服务器的 Java 客户端,例如网络时间协议 (NTP)。
Radio Clock
无线电时钟
Obtain a radio clockwith a USBconnection for output of current time synchronized by a time code transmitted by a radio transmitter connected to a time standard such as an atomic clock. Transmitters are broadcasting in many countries all over the world.
获取一个带有USB连接的无线电时钟,用于输出与时间标准(如原子钟)相连的无线电发射器传输的时间码同步的当前时间。发射机正在世界上许多国家广播。
The Meinberg Globalcompany, at least, offers several such devices.
该Meinberg全球公司,至少,提供了几个这样的设备。
GPS
全球定位系统
Similar to the radio clocks above, a receiver of GPS (Global Positioning System) signals might also capture and relay the time signal. Or perhaps GALILEO, GLONASSor other Radionavigation-satellite service.
与上面的无线电时钟类似,GPS(全球定位系统)信号的接收器也可能捕获并中继时间信号。或者GALILEO、GLONASS或其他无线电导航卫星服务。
Sundial
晷
Position a sundialoutside a window. Attach a webcamto the computer in question. Position the webcam in the window. Write an app to interpret the time of day from current image of the sundial.
将日晷放在窗外。将网络摄像头连接到相关计算机。将网络摄像头放置在窗口中。编写一个应用程序,从日晷的当前图像解释一天中的时间。
Caveat:This solution does not function in Seattle.
警告:此解决方案在西雅图不起作用。
java.time.Clock
java.time.Clock
To harness any of these suppliers of the current moment, write a subclass of the abstract java.time.Clock
class.
要利用当前时刻的这些供应商中的任何一个,请编写抽象java.time.Clock
类的子类。
You can pass your Clock
implementation as an argument to the various java.timemethods. For example, Instant.now( clock )
.
您可以将您的Clock
实现作为参数传递给各种java.time方法。例如,Instant.now( clock )
。
Instant instant = Instant.now( yourClockGoesHere ) ;
For testing purposes, note the alternate implementations of Clock
available statically from Clock
itself: fixed
, offset
, tick
, and more.
出于测试目的,注意的可选的实施方式Clock
从现有的静态Clock
本身:fixed
,offset
,tick
,和更多。
Avoid the legacy date-time classes from the earliest versions of Java, such as Date
& Calendar
. These troublesome classes are entirely supplanted by the java.timeclasses.
避免使用最早版本的 Java 中遗留的日期时间类,例如Date
& Calendar
。这些麻烦的类完全被java.time类取代。
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*
classes.
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android,ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。
回答by KayV
You can use the following code:
您可以使用以下代码:
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int date = localDate.getDayOfMonth();