- 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
Java 8 Clock instant() method
The current instant of the clock object can be obtained using the method instant() in the Clock Class in Java. This method requires no parameters and it returns the current instant of the clock object. If the instance cannot be obtained for some reason, then the DateTimeException is thrown.
A program that demonstrates this is given as follows −
Example
import java.time.*; public class Demo { public static void main(String[] args) { Clock c = Clock.systemDefaultZone(); Instant i = c.instant(); System.out.println("The clock is: " + c); System.out.println("The instance is: " + i); } }
Output
The clock is: SystemClock[Etc/UTC] The instance is: 2019-02-07T08:54:18.679Z
Now let us understand the above program.
The method instant() is used to obtain the current instance of the clock and then this is displayed. A code snippet that demonstrates this is as follows −
Clock c = Clock.systemDefaultZone(); Instant i = c.instant(); System.out.println("The clock is: " + c); System.out.println("The instance is: " + i);
Advertisements