Java

Java Clock – instant()

instant() method in Java time is used to return the instance of the clock in the timestamp format.

We can use instant() methods with different Java time methods to get the Timezone information. It doesn’t take any parameters.

Syntax

 

clock_object.instant()

 
clock_object represents the clock that has clock information.

Return

Time Stamp

We will see the usage of instant() methods with different methods.

Scenario 1: instant()

Here, we will directly use the instant() method on the clock object.

Example 1:

Let’s create the clock_object with the default zone and return the TimeStamp using instant() method.

import java.time.Clock;
public class Main
{
    public static void main(String[] args) {
       
        // Create the clock object named clock_object
        Clock clock_object = Clock.systemDefaultZone();
       
        System.out.println("Clock Details: "+clock_object);
       
        // Return the TimeStamp
        System.out.println("TimeStamp Details: "+clock_object.instant());
       
        }
}

 

Output

 

Explanation

Line 7:

Create the clock object named clock_object.


Line 12:

Use the instant() method to return the TimeStamp of the Clock object.

Example 2:

Let’s create the clock_object with the Portugal timezone and return the TimeStamp using instant() method.

Here, we have to pass the ZoneId.of(“Portugal”) parameter in the Clock.system() object.

import java.time.Clock;
import java.time.ZoneId;

public class Main
{
    public static void main(String[] args) {

        // Create the clock object named clock_object with ZoneId - Portugal
        Clock clock_object = Clock.system(ZoneId.of("Portugal"));
       
        System.out.println("Clock Details: "+clock_object);
 
        // Return the TimeStamp from the clock_object
        System.out.println("TimeStamp: "+clock_object.instant());
         
        }
}

 

Output

 

Explanation

Line 8: Create the clock object named clock_object with Zone – Portugal.


Line 10: Return the Clock object.


Line 14: Return the TimeStamp  using the instant() method.

Scenario 2: instant() with atZone() and getZone()

If you want to return the Zoned datetime object, you can use atZone() with instant() method.

Syntax

 

clock_object.instant().atZone(clock_object.getZone())

 

Example 1:

Let’s return the date and time for the TimeZone – Portugal.

import java.time.Clock;
import java.time.ZoneId;

public class Main
{
    public static void main(String[] args) {

        // Create the clock object named clock_object with ZoneId - Europe/Paris
        Clock clock_object = Clock.system(ZoneId.of("Portugal"));
       
        System.out.println("Clock Details: "+clock_object);
 
        // Return the date & time of the clock_object
        System.out.println(clock_object.instant().atZone(clock_object.getZone()));
         
        }
}

 

Output

 

Explanation

Line 8: Create the clock object named clock_object with Zone – Portugal.


Line 10: Return the Clock object.


Line 14: Return the date & time of the clock_object using instant() method.

Example 2:

Let’s return the date and time for the TimeZone – Bissau.

import java.time.Clock;
import java.time.ZoneId;

public class Main
{
    public static void main(String[] args) {

        // Create the clock object named clock_object with ZoneId - Africa/Bissau
        Clock clock_object = Clock.system(ZoneId.of("Africa/Bissau"));
       
        System.out.println("Clock Details: "+clock_object);
 
        // Return the date & time of the clock_object
        System.out.println(clock_object.instant().atZone(clock_object.getZone()));
         
        }
}

 

Output

 

Explanation

Line 8: Create the clock object named clock_object with Zone – Bissau.


Line 10: Return the Clock object.


Line 14: Return the date & time object  of the clock_object using instant() method with atZone() and getZone().

Conclusion

In this DateTime Java tutorial, we saw how to use the instant() method with scenarios like getZone() and atZone(). instant() is used to return the current instance if a clock in a TimeStamp format. If you want to get the datetime object, then you can use atZone() and getZone() methods with instant() method.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain