Convert System.nanoTime to Seconds in Java

Introduction

In this article, we will look into How to Convert System.nanoTime() to Seconds in Java. We will look at different solutions to this problem in detail with working examples. Let us first have a quick look into the System.nanoTime() method in Java.

System.nanoTime()

The System.nanoTime() method is a static method of the System class and it gives the current value of the JVM’s most precise or high-resolution time source in nanoseconds. The value returned is of very high precision around 1/1000000th of a second.

The syntax of the method:

It returns the value of type Long. It is not thread-safe, the accuracy decreases if used between more than two threads.

Now, we outline different ways to Convert System.nanoTime() to Seconds in Java.

Dividing the System.nanoTime() with a Constant Value

To convert System.nanoTime to Seconds, we can divide the nanoseconds value returned by the nanoTime() method with a constant value of  1_000_000_000, this division will give the resultant value including decimals which must be typecasted to type double.

Let us look into the code.

Output:

Using the convert() Method of Time Unit Class in Java

The TimeUnit class in Java represents time durations at a given unit and provides useful utility methods to convert across these units. It allows conversions to  Minutes , Seconds, Milliseconds, Nanoseconds and other units. It provides the convert() method that different durations across these time units.

The syntax of method:

  • Use the Static reference to the TimeUnit class to convert the System.nanoTime() and specify the timeunit to convert in this case we provide it as : SECONDS.
  • Pass sourceDuration as System.nanoTime() to convert() method.
  • Pass sourceUnit as TimeUnit.NANOSECONDS as SourceDuration is in Nano seconds Timeunit.

Let us look at the implementation in code.

Output:

In order to obtain a high precision value than integer, we can convert this value to type double to get higher precision up to 3 decimal places.

  • In such case we need to get this value in format TimeUnit.MILLISECONDS.
  • Then divide the value by 1000.0 to convert it into seconds and get the value with precision of up to 3 decimal places.

Let us look at the code snippet.

Output:

Using the toSeconds() Method of Time Unit Class in Java

The TimeUnit class also provides the toSeconds() method that converts a given time duration unit to seconds. In order to convert the value, we need to specify the type of Source Time duration for e.g. NANOSECONDS , MILLISECONDS, etc.

The syntax of the method:

Let us look into the code.

Output:

Using the Utility Methods of Duration Class in Java

With the advent of Java 9, the Duration class was modified and some new features were introduced, this class models a unit of time in terms of seconds and nanoseconds and provides useful and common utility methods to perform operation on these time units.

It is present in java.time package library. Here, we can use the utility methods of these class to Convert System.nanoTime() to seconds.

  • The Duration class provides the getSeconds() method and toSeconds() method respectively to convert a time unit to its equivalent value in seconds.
  • Use the ofNanos() method of the Duration class to specify our time unit to be in format of Nanoseconds and pass the System.nanotime() as a argument to this method.

The syntax of the methods :

Let us look at the implementation of this approach in code.

Output:

That’s all about the article we listed 4 different ways on How to Convert System.nanoTime() to Seconds in Java in detail with working examples. You can try them out with code in your Local IDE for a clear understanding.

Feel free to reach out to us for any queries/suggestions.

Was this post helpful?

Related Posts

  • Rename Files with PowerShell
    18 January

    Rename Files with PowerShell

    Table of ContentsRename Single File with PowerShellUsing Rename-Item with/without the -Path ParameterUsing Rename-Item with -Confirm ParameterUsing Rename-Item with -Passthru ParameterRename multiple files in PowerShell Rename Single File with PowerShell Using Rename-Item with/without the -Path Parameter Use the Rename-Item cmdlet to rename a file in PowerShell. [crayon-63ce565180de9929509012/] We used the Rename-Item cmdlet to rename the test.txt […]

  • PowerShell - redirect output to File
    18 January

    PowerShell – Redirect Output to File

    Table of ContentsUsing Out-File CmdletUsing Redirection OperatorsUsing StreamWriter .Net ClassUsing Add-Content CmdletUsing Set-Content Cmdlet Using Out-File Cmdlet Use the Out-File cmdlet to redirect PowerShell output to a file. [crayon-63ce565180f52258475632/] The Get-Process is used to retrieve all the processes that are currently running on our machine (local computer). We can filter this information by specifying parameters; […]

  • PowerShell - Logical operators
    18 January

    PowerShell – Logical Operators

    Table of ContentsUsing Logical Operators in PowerShellUse Logical AND OperatorUse Logical OR OperatorUse Logical EXCLUSIVE OR OperatorUse Logical NOT OperatorUse Multiple Logical Operators Using Logical Operators in PowerShell PowerShell’s logical operator is a potent tool for comparing data and carrying out operations depending on those comparisons. It allows the user to design an if-then scenario […]

  • Show hidden files in PowerShell
    13 January

    Show Hidden Files (dotfiles) with Windows PowerShell

    Table of ContentsUsing Get-ChildItem with the -Force ParameterUsing Get-ChildItem with the Where-Object cmdletUsing ls with the -Force ParameterUsing dir with -ah Switch Using Get-ChildItem with the -Force Parameter To show hidden files with other files, use the Get-ChildItem cmdlet with the -Force or -fo parameter. [crayon-63ce565181184130997358/] [crayon-63ce565181186498722021/] The Get-ChildItem is a PowerShell cmdlet that lists […]

  • Merge multiple CSV files in PowerShell
    13 January

    Merge Multiple CSV Files in PowerShell

    Table of ContentsUsing Import-Csv and Export-CsvUsing + operatorUsing the File Class Using Import-Csv and Export-Csv Before merging multiple .csv files, let’s write a script to see how many .csv files are in the specified directory. For that, create a variable called $sourcefiles containing a list of all the CSV files in the directory. Next, use […]

  • Multiline command powershell
    10 January

    PowerShell – Multiline Command

    Table of ContentsUsing BackticksUsing Pipeline OperatorUsing Specific Operators/Characters Using Backticks Use backticks to split multiline commands in PowerShell. [crayon-63ce56518145d642067618/] Usually, we get an automatic line continuation in PowerShell when the command can’t syntactically be complete. However, if splitting a multiline command is required, then using backticks is preferred in such scenarios as presented above where […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.