Getting Future Time

Discussion in 'Plugin Development' started by Hydrosis, Jan 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Hydrosis

    Hello,

    I'm having some trouble getting a time in the future. I have no idea why java is giving me this output for the code I put in.

    My Code:
    Code:java
    1. long start = System.nanoTime();
    2. long end = start+30*1000000000;
    3. long elapsedTime = end - start;
    4. double seconds = (double)elapsedTime / 1000000000.0;
    5. System.out.println("Time Elapsed: " + elapsedTime);
    6. System.out.println("In Second: " + seconds);


    Output:
    Code:
    Time Elapsed: -64771072
    In Second: -0.064771072
    
    Why is it giving me a negative number if end is supposed to be larger than start?
     
  2. Offline

    Rocoty

    Read Oracle's documentation about nanoTime(). I think you might find it most interesting
     
  3. Offline

    Hydrosis

    Ah I see. So nanoTime should only be used to measure time elapsed, while currentTimeMillis should be used for specific times. Thanks!

    Ok, so I have the exact same lines of code producing different outputs...
    Here my code:
    Code:java
    1. long currentTime = System.currentTimeMillis();
    2. long nextTime = currentTime+TimeUnit.SECONDS.toMillis(30);
    3. System.out.println("Current time: " + currentTime);
    4. System.out.println("Next Time: " + nextTime);
    5. System.out.println("Diff: " + (nextTime-currentTime));
    6. System.out.println("Next runn will be in " + TimeUnit.MILLISECONDS.toSeconds(nextTime-currentTime));

    Here is the output when I run this as a plugin (I'm using spigot):
    Code:
    [12:52:23 INFO]: Current time: 1332909182835801
    [12:52:23 INFO]: Next Time: 1332909118056176
    [12:52:23 INFO]: Diff: -65146979
    [12:52:23 INFO]: Next runn will be in 0
    Here is the output when I run this as a console application
    Code:
    Current time: 1390243954497
    Next Time: 1390243984497
    Diff: 30000
    Next runn will be in 30
    Another thing to note, the "Diff" value when i run it in a plugin produces a different number each time. Any ideas why there is this inconsistency? :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page