Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8029231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:22:31+00:00 2026-06-05T00:22:31+00:00

I wish to calculate the time passed in milliseconds from a specific time in

  • 0

I wish to calculate the time passed in milliseconds from a specific time in Java.

The classic way it to use System.currentTimeMillis(); for the starting time, and then use this again with the previous one to get the elapsed time. I wish to do something similar to this, but NOT rely on the system time for this.

If I rely on the system time, the user of the program could manipulate the system clock to hack the program.

I have tried using code similar to the following:

int elapsed = 0;
while (true) {
    Thread.sleep(10);
    elapsed += 10;
}

This works, but I it is not too reliable in the case that the computer lags and then locks up for a second or two.

Any ideas anyone?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:22:32+00:00Added an answer on June 5, 2026 at 12:22 am

    You want to utilize System.nanoTime. It has no relation to the system clock. It can only be used to track relative time which seems to be all you want to do.

    In an effort to prevent this answer from just being a link to another answer here is a short explanation.

    From Documentation

    public static long nanoTime() Returns the current value of the most
    precise available system timer, in nanoseconds.

    This method can only be used to measure elapsed time and is not
    related to any other notion of system or wall-clock time. The value
    returned represents nanoseconds since some fixed but arbitrary time
    (perhaps in the future, so values may be negative). This method
    provides nanosecond precision, but not necessarily nanosecond
    accuracy. No guarantees are made about how frequently values change.
    Differences in successive calls that span greater than approximately
    292 years (263 nanoseconds) will not accurately compute elapsed time
    due to numerical overflow.

    Yet another link to timer information: https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks

    You could use Java’s Timer class to spawn a “check” callback at some specific precision, lets say every 500ms. This callback would not be used to determine that 500ms actually did pass. You would call System.nanoTime in the callback and compare it to the last time you called System.nanoTime. That would give you a fairly accurate representation of the amount of time that has passed regardless of the wall clock changing.

    You can take a look here: System.currentTimeMillis vs System.nanoTime

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to calculate position of a small remote controlled car (relative to starting
I am trying to use jQuery to calculate the window height then apply that
I wish to calculate the time it took for an API to return a
I wish to write a C program which obtains the system time and hence
I wish to edit ini files over web server, decided to use django, been
I wish to move backwards - lets say im in Backgrounds node. Then i
Ok guys, I am using the following library: http://www.codeproject.com/KB/recipes/AdvancedMatrixLibrary.aspx And I wish to calculate
I wish to use Entity Framework Code-first for a new project. So i decided
I'm working on a real-time isometric RPG in python, and wish to target mobile
I wish to calulate the item in the warehouse and calculate the free space

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.