Java 8 Clock instant() method

Java 8ProgrammingObject Oriented Programming

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

 Live Demo

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);
raja
Published on 13-Mar-2019 15:40:39
Advertisements