tags:

views:

90

answers:

3

I have written a method using JSF:

public static String getCurrentTime(Locale locale) {
        Calendar cal=Calendar.getInstance(locale);
        return new SimpleDateFormat("HHmmss").format(cal.getTime());
    }

While testing my application in my local machine I am getting the time same as system time but when my friends are testing it time delay is of 4 minutes.

What may be the problem? else suggest a code to do the same

+4  A: 

This is happening because you are testing on two different machines - your system time is not the same.

The time is taken strictly from your local computer time. If you set the clock to 12:34, you will get 12:34, and not the real time.

Unless you connect to a time server on the network, or somehow manage to sync your clocks, you can never guarantee you will get the same time.

Alternatively, once you deploy your JSF application to a production server, you will always get the server time, which naturally is guaranteed to be correct for all users.

Yuval A
I agree. This has nothing to do with Java or Java Server Faces, per se. It's just an issue of time synchronization.
Matthew Flaschen
+3  A: 

If you want the time to be accurate, synchronize the clocks of the computers with NTP.

Esko Luontola
A: 

You could check the computers both match a clock on the web like http://www.atomic-clock.org.uk/atomuhr.html

Peter Lawrey