如何在Java中获取当前服务器时间? [英] How to get current server time in Java?

查看:1146
本文介绍了如何在Java中获取当前服务器时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中获取当前服务器的时间?我尝试了 System.currentTimeMills()但它返回的是客户端的时间而不是服务器时间。我希望以毫秒为单位获得服务器时间。

How can I get current server's time in Java? I tried System.currentTimeMills() but it is returning me the client's time not server time. I want to get "Server's" time in milliseconds.

请不要关闭此问题。我在网上搜索,但我不明白。所有代码都从 1970 返回值。我想要的是例如:当前服务器时间 9/25/2012:10:19AM 以毫秒为单位。

Please don't close this question. I searched in net, but I dind't get. All the code returns value from 1970. What I want is eg: Current server time is 9/25/2012:10:19AM in milliseconds.

我也搜索了其他问题,这与其他问题没有重复。

Also I searched other questions and it is not duplicate of other questions.

推荐答案

System.currentTimeMillis() 返回自1970年以来的毫秒数。

System.currentTimeMillis() returns number of milliseconds since 1970.

如果在服务器上运行它,您将获得服务器时间。如果您在客户端上运行它(例如Applet,独立桌面应用程序等),您将获得客户端时间。要在客户端上获取服务器日期,请设置一个调用,将格式化字符串中的服务器时间返回给客户端。

If you run it on the server, you will get the server time. If you run it on the client (e.g. Applet, standalone desktop app, etc.) you will get the client time. To get the server date on the client, set up a call that returns the server time in the formatted string to the client.

如果您希望日期格式化为某种方式,首先创建一个 Date()对象,然后使用 SimpleDateFormat

If you want the date formatted a certain way , first create a Date() object and then format it using SimpleDateFormat

Date d1 = new Date();
SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY HH:mm a");
String formattedDate = df.format(d1);

这篇关于如何在Java中获取当前服务器时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆