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.
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.
System.currentTimeMillis()
returns number of milliseconds since 1970.
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.
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);
If you want your client to get the server's time, you will have to make a request to the server and have the server send back its current time in the response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With