-4

I want nanoTime() of last 24 hours. long currentDate=System.nanoTime(); how to find nanoTime by subtracting 24 hours? Is it possible to get nanoTime from Date date = new Date()?

6
  • 2
    You can subtract 24 hours from nanoTime if you want to do that I guess? System.nanoTime() - (long) (8.64e+13); No idea what you would use that for though. Commented Jun 14, 2018 at 10:58
  • 1
    Nanotime is the time since your computer was switched on, and it cycles round back to negative values through zero and then positive again, if left on for long enough. It has nothing to do with real-world time. Why do you want nanotime of 24 hours ago? Commented Jun 14, 2018 at 11:08
  • I am saving currentTime as nanoTime in database. I want last 24 hrs nanoTime because want to fetch report with only last 24 hours records Commented Jun 14, 2018 at 11:19
  • 3
    Don’t do that. When you computer is shut down or rebooted, your stored times are useless. They may even be when you program ends. If values from two or more computers are stored they also are. From the docs: “This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.” Commented Jun 14, 2018 at 11:49
  • Why not just record the ticks count of the current time (new java.util.Date().getTime(), which is accurate to milliseconds, and simply subtract 24 hours worth of milliseconds (24*60*60*1000L) from it? Commented Jun 14, 2018 at 15:01

1 Answer 1

3

System.nanoTime() cannot be used for current time. It is “is not related to any other notion of system or wall-clock time”, as the docs say. This also means that getting a compatible nano time from a Date would not make sense.

Instead use java.time.Instant for your times. Instant.now() gives you the current time, and from Java 9 it has a very high resolution on most platforms (microseconds on my Mac, I believe it’s just a good as nanoTime). Then use for example yourInstant.minus(1, ChronoUnit.DAYS) or yourInstant.minus(Duration.ofHours(24)).

I need to store long value in database.

Edit: I’d prefer to store the Instant as such into the database, or if that’s not possible, then its string representation as you get it from its toString method (the Instant class can parse this string back into an Instant). Since I understand that you’re required to save a long value: This will be possible until 2262-04-11T23:47:16.854775807Z, the next more than 200 years. If this is good enough, here’s how to get nanoseconds since the epoch as a long value from an Instant:

 Instant instantNow = Instant.now(); long now = Math.addExact( Math.multiplyExact(instantNow.getEpochSecond(), 1_000_000_000), instantNow.getNano()); 

The point in using addExact and multiplyExact from the Math class rather than plain + and * is you will be notified in case of overflow, that is, in case the value doesn’t fit into a long.

is it possible to convert date to Instant, as we have search by date function on UI[?]

To convert a date (like December 25, 2017) to an Instant you need to decide a time zone for the conversion because it is never the same date everywhere on Earth. For example:

 ZoneId zone = ZoneId.of("Europe/Brussels"); LocalDate date = LocalDate.of(2017, Month.DECEMBER, 25); Instant asInstant = date.atStartOfDay(zone).toInstant(); 

To answer your original question (doubting that you can use it), to subtract 24 hours from System.nanoTime():

 long nanoYesterday = System.nanoTime() - TimeUnit.DAYS.toNanos(1); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Instant is working. I need to store long value in database. If I store Instant Object to db , is it possible to convert date to Instant, as we have search by date function on UI.I will get date object. So i will need to convert it to Instant for comparison.
Please see my edit, @AmrutaH
@AmrutaH do you actually need to use nanoseconds? Or do you only need some time value as a long? If it's the latter, why not just use System.currentTimeMills()? (Or equivalently Instant.toEpochMilli()?)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.