- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Duration ofNanos() method in Java
The duration can be obtained in a one nano second format using the ofNanos() method in the Duration class in Java. This method requires a single parameter i.e. the number of nano seconds and it returns the duration in a one nano second format. If the capacity of the duration is exceeded, then the ArithmeticException is thrown.
A program that demonstrates this is given as follows −
Example
import java.time.Duration; public class Demo { public static void main(String[] args) { long nanoseconds = 1000000000; Duration duration = Duration.ofNanos(nanoseconds); System.out.println("The duration in seconds is: " + duration.getSeconds()); } }
Output
The duration in seconds is: 1
Now let us understand the above program.
The duration in a one nano second format is obtained using the ofNanos() method. Then this duration is printed in seconds using the getSeconds() method. A code snippet that demonstrates this is as follows −
long nanoseconds = 1000000000; Duration duration = Duration.ofNanos(nanoseconds); System.out.println("The duration in seconds is: " + duration.getSeconds());
Advertisements