- 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 get() method in Java
The value of the chronological unit can be obtained using the method get() in the Duration class in Java. This method requires a single parameter i.e. the TemporalUnit for which the value is required. Also, the value of the chronological unit is returned by this method.
A program that demonstrates this is given as follows −
Example
import java.time.Duration; import java.time.temporal.*; public class Demo { public static void main(String[] args) { Duration d = Duration.ofMinutes(2); System.out.println("The duration in minutes is: " + d); long seconds = d.get(ChronoUnit.SECONDS); System.out.println("The duration in seconds is: " + seconds); } }
Output
The duration in minutes is: PT2M The duration in seconds is: 120
Now let us understand the above program.
The value of the chronological unit is obtained using the method get() and then this is printed. A code snippet that demonstrates this is as follows −
Duration d = Duration.ofMinutes(2); System.out.println("The duration in minutes is: " + d); long seconds = d.get(ChronoUnit.SECONDS); System.out.println("The duration in seconds is: " + seconds);
Advertisements