Calculating time difference in Milliseconds

前端 未结 9 544
谎友^
谎友^ 2020-12-04 21:42

I am making a call to a method by passing ipAddress and it will return back the location of ipAddress like Country, City, etc etc. So I was trying to see how much time it is

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 22:17

    I pretty much like the (relatively) new java.time library: it's close to awesome, imho.

    You can calculate a duration between two instants this way:

    import java.time.*
    
    Instant before = Instant.now();
    // do stuff
    Instant after = Instant.now();
    long delta = Duration.between(before, after).toMillis(); // .toWhatsoever()
    

    API is awesome, highly readable and intuitive.

    Classes are thread-safe too. !


    References: Oracle Tutorial, Java Magazine

提交回复
热议问题