stuckedunderflow
stuckedunderflow

Reputation: 3767

Java String Date updated per minute locally

I have asked web service to return current time in city X. Then it return it as a String. E.g: 2012-11-24 19:30

Then I want to keep it running locally, means I need to update this time at least per minute. Obviously I don't want user to always make a request to web service to get the real time per minute.

Any good, efficient and effective idea how to implement this?

Thanks

Upvotes: 0

Views: 188

Answers (2)

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

As recommended by @Max using timezone offset is what you really want to do, however to answer your literal question you may try something like:

Date remoteDate = new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(string);
long offset = new Date().getTime() - remoteDate.getTime();

Now whenever want to get the current remote Date:

Date currentRemoteDate = new Date(new Date().getTime() + offset);

Upvotes: 1

someone
someone

Reputation: 6572

Use java websocket. in each minute, time will post to websocket connection and connection will tel to client update the time. Check the client server example and you could get idea of how websocket works.

(For this you need html5 support web browsers)

Upvotes: 0

Related Questions