Syncing clocks

I have a host (asgard) where the java app is pulled from. I want the time on asgard to be displayed in the app which is running on remote machines. I currently have a clock in my app, so that's not the issue -- the issue is making the two be the same. Changing the system time of te remote machine isn't an option -- it has to be contained 100% in the app. What I can do (and was planning on doing) is having my app run a simple perl script on asgard that returns asgard's system time in epoch. What I need to know, I guess, is how I can create my own date/calendar object that will use this epoch returned, set it as the starting point, and then count like a normal clock. The perl script can only be run occasionally, so calling it every cycle through my app to update it is also not an option. Any ideas on how to approach this? (I don't need code, just ideas/methods to deal with it)

Thanks a bunch in advance!

Justin

[951 byte] By [dcmrc0] at [2007-9-26 2:48:15]
# 1

I'm not sure how useful this would be, but maybe you could get the value returned, set a Date object to that value, then start a thread that sleeps for 1000 milliseconds and then increments the date field in a continuous loop. I'm guessing this would be fairly inaccurate, since thread priority and CPU usage would have an impact on actual sleep time, but if you can run the perl script fairly often, you could reset each time.

Or since you already have a clock in your app, maybe you could take the difference between your app's time and the returned time and instead of displaying your app's time, display your app's time + or - the difference between the two.

Rich

rmiller1985 at 2007-6-29 10:33:26 > top of Java-index,Archived Forums,Java Programming...
# 2

Having a seperate thread that sleeps for 1000ms isn't really an option here. These clocks need to be _very_ close (+/- 1sec MAX). I can't call the perl script except when the app loads, so correction wouldn't exist.

I was thinking about doing the difference method you described, but I wasn't sure of how to approach it -- the Date and Calendar objects are ones I've never dealt with before and I've had very little luck dealing with now.

The way I have it currently set up (and I hope this clarifies things more) is...

Date timeNow = new Date();

String curDate = timeNow.toLocaleString();

...and then I display it later (and update it with "curDate = (new Date()).toLocaleString();" every cycle through (which turns out to be roughly 0.2 seconds, so it's pretty much always right on top of the time).

What I _think_ I need is a way to change the Calendar that "new Date()" uses to grab the time....

I could be terribly off on this concept, so if I am, please tell me. I've tried things as simple as...

long test = 901101010;

Date newDateToUse = new Date(test);

Calendar.setTime(newDateToUse);

...just so I could test to see if this approach would work, but I end up getting "Identifier expected" or "Can't make static reference to method void setTime(java.util.Date) in java.util.Calendar" errors (depending on the placement).

Thanks for your quick response; anyone else?

Justin

dcmrc0 at 2007-6-29 10:33:26 > top of Java-index,Archived Forums,Java Programming...
# 3

Well, here's what I was thinking of:

import java.util.*;

public class Test {

public static void main (String[] args) {

Date loopDate;

Date printDate;

// The actual value for inputDate would come from your server.

Date inputDate = new Date();

// For testing purposes, get a (not-quite) random difference.

try {

Thread.sleep(28583);

} catch (InterruptedException ie) {

// Thread interrupted.

}

// Get the time from your app to compute the difference.

Date currentDate = new Date();

// Compute the difference.

long dateDifference = currentDate.getTime() - inputDate.getTime();

System.out.println(dateDifference);

// Start a loop where you update the printed date.

loopDate = new Date();

printDate = new Date(loopDate.getTime() - dateDifference);

System.out.println(loopDate);

System.out.println(printDate);

}

}

I didn't actually put the loop part in a loop, but I think you get the idea. I don't know whether this would be appropriate or not, since I'm not familiar with the application, but it's what came to mind.

Rich

rmiller1985 at 2007-6-29 10:33:26 > top of Java-index,Archived Forums,Java Programming...
# 4

And incidentally, you're probably getting the Calendar errors because Calendar is an abstract class. You want to use a GregorianCalendar object. You can get it with

GregorianCalendar gc = new GregorianCalendar();

or

Calendar gc = Calendar.getInstance();

(I think I have that last one right, now I have to go check!)

Rich

rmiller1985 at 2007-6-29 10:33:26 > top of Java-index,Archived Forums,Java Programming...
# 5

So dcmrc0, does this work for you?

I'm actually interested in other ideas as well. I'm writing a little alarm clock application to practice coding, and I was going to try to follow the time without using the actual date (since the user has to be able to set the time). I was planning on using something similar to what I posted, but I'd like to hear other ideas.

Rich

rmiller1985 at 2007-6-29 10:33:26 > top of Java-index,Archived Forums,Java Programming...