how to make my java app get global time from some online clock

前端 未结 5 1737
温柔的废话
温柔的废话 2020-12-07 02:51

I am making a distributed java app for which I need both parts of the app to run on one standard time. Since system times can be different I was thinking if java API contain

5条回答
  •  庸人自扰
    2020-12-07 03:18

    pay attention .... timeInfo.getReturnTime() does not return the current time from the timeserver. it returns the local time when the request to the server was made.

    after calling timeInfo.computeDetails() it's possible to get the offset by timeInfo.getOffset(). this returns the offset of the local time in millisecond.

    to calculate the current time you could do something like:

    ...
    long systemtime = System.currentTimeMillis();
    Date realdate = new Date(systemtime + timeInfo.getOffset());
    ...
    

提交回复
热议问题