0
  long nanoseconds = System.nanoTime();
  long microseconds = nanoseconds / 1000;
  long miliseconds = microseconds / 1000;
  long seconds = miliseconds / 1000;
  long minutes = seconds / 60;
  long hours = minutes / 60;

  System.out.println (hours);

When ran at ~11:35 am on my Windows machine, prints 26. There can't be 26 hours in a day and even if there were, it's not even close to the expected result 11. Why doesn't this work? (Also I don't want to use any Time Date library, I know there are easier ways out there)

Hope this all formats correctly since I'm on my phone. Thanks everyone!

Hatefiend
  • 3,416
  • 6
  • 33
  • 74
  • 1
    you didn't carry the calculations far enough. `days = hours / 24`? – Marc B Jan 11 '16 at 16:54
  • "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 origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin." – Steve-o Jan 11 '16 at 16:54

1 Answers1

4

From the Oracle Documentation you can read this:

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.

So you shouldn't use this method.

Gianni B.
  • 2,691
  • 18
  • 31
  • Oh okay thank you. So I'm looking for accuracy and speed when it comes to printing out a time stamp so I figured using a time date object would be a bad idea. What do you recommend? – Hatefiend Jan 11 '16 at 17:00
  • 1
    What about `System.currentTimeMillis()` ? – Gianni B. Jan 11 '16 at 17:01
  • So System.currentTimeMillis() works in this way but NanoTime() doesnt? – Hatefiend Jan 11 '16 at 17:02
  • Maybe describe your specific requirement in more detail in your original question - what do you mean by "_accuracy and speed_"? If speed is really that big of a deal, Java's probably not your ideal tool. If you're talking about some kind of web app that you want to be as quick as possible, a proper Date/Calendar/Instant/Timestamp object isn't going to be slow enough to made a noticeable difference. – DaveyDaveDave Jan 11 '16 at 17:03
  • @Hatefiend If you really want the time ATM in milliseconds, you have to use the `Calendar` class. `Calendar calendar = Calendar.getInstance(); long timeMilliseconds = calendar.getTimeInMillis();` – Gianni B. Jan 12 '16 at 07:52
  • But I don't need all the features of a calendar. I just need the clock, that's it. I don't want to waste memory on leap years, leap seconds, Gregorian calendar calculations, etc. – Hatefiend Jan 12 '16 at 20:09
  • @Hatefiend So, [looking at this table from Java Documentation](http://docs.oracle.com/javase/tutorial/datetime/iso/overview.html), your class to go is [LocalTime](https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html) but you need to use Java 8 – Gianni B. Jan 13 '16 at 07:35
  • 1
    @GianniB. Thanks! I had put in `LocalDateTime` but you're right, `LocalTime` is better for this. Appreciate the help! – Hatefiend Jan 13 '16 at 22:36