Java 8 Clock instant() method with Examples

Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.

instant() method of Clock class returns a current instant of Clock object as Instant Class Object. Instant generates a timestamp to represent machine time. So this method generates a timestamp for clock object. Here returned Instant is Object of java.time.Instant class which represents a specific moment on the time line in UTC Zone. This timeline is a count of nanoseconds since the epoch of the first moment of 1970 UTC. Since nowadays most of business logic, data storage, and data exchange should be in UTC, so using Instant is useful.

Syntax:

public abstract Instant instant()

Return Value: This method returns the current instant of clock object.

Exception: This method throws a DateTimeException if the instant of clock object cannot be obtained.



Example:

Input:: 
a clock class Object e.g Clock.systemDefaultZone()

Output::
instant  e.g. 2018-08-19T20:22:23.366Z

Explanation:: 
when instant() is called, it returns a current instant of Clock Class Object. 

Below programs illustrates instant() method of java.time.Clock class:

Program 1: Get Clock object with systemDefaultZone using instant()

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate
// instant() method of Clock class
  
import java.time.*;
  
// create class
public class instantMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create Clock Object
        Clock clock = Clock.systemDefaultZone();
  
        // get Instant Object of Clock
        // object using instant() method
        Instant instantObj = clock.instant();
  
        // print details of Instant Object
        System.out.println("Instant for class " + clock
                           + " is " + instantObj);
    }
}

chevron_right


Output:

Instant for class SystemClock[Etc/UTC] is 2018-08-21T05:31:10.662Z

Program 2: Get Clock object with Zone “Europe/Paris” using instant()

To get zonal based date and time, get ZonedDateTime object from instant by using atZone(ZoneId zone) to print date and time of that Zone.

Syntax:

// get ZonedDateTime object from instant object returned by instant() method of Clock class
ZonedDateTime time = Clock.systemDefaultZone().instant().atZone(Clock.getZone());

Code:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate
// instant() method of Clock class
  
import java.time.*;
  
// create class
public class instantMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create a Zone Id for Europe/Paris
        ZoneId zoneId = ZoneId.of("Europe/Paris");
  
        // create Clock Object by passing zoneID
        Clock clock = Clock.system(zoneId);
  
        // get Instant Object of Clock
        // object using instant() method
        Instant instantObj = clock.instant();
  
        // get ZonedDateTime object from
        // instantObj to get zonal date time
        ZonedDateTime time = instantObj.atZone(clock.getZone());
  
        // print details of Instant Object
        System.out.println("Instant for class " + clock
                           + " is " + time.toString());
    }
}

chevron_right


Output:

Instant for class SystemClock[Europe/Paris] is 2018-08-21T07:31:13.525+02:00[Europe/Paris]

Refrence:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#instant–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.