开发者

Time stamp with milliseconds, calculating "delay"

开发者 https://www.devze.com 2023-01-25 08:49 出处:网络
When i create an instance of an object, say, Message m i want to set a field in it, a time stamp. Nothing to do with databases.

When i create an instance of an object, say, Message m i want to set a field in it, a time stamp.

Nothing to do with databases.

When this object is passed around it will have its time stamp eventually read and compared to the current time of its recipient to calculate de开发者_JAVA百科lay = timeNow - m.SENT_AT. Both objects exist on the same system and both entities use the system's clock (actual time, nothing fancy, the delay is important), but I want this in milliseconds.


Something like this:

class Message {
    public final long sentAt; 
    public final String data;

    public Message(String data) {
        this.sentAt = System.currentTimeMillis()
        this.data = data;
    }

    public long timeSinceSent() {
        return System.currentTimeMillis() - sentAt;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号