Wednesday, July 3, 2024
HomeLanguagesJavaJava 8 Clock instant() method with Examples

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 timeline in UTC Zone. This timeline is a count of nanoseconds since the epoch of the first moment of 1970 UTC. Since nowadays most of the 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() 

Java




// 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);
    }
}


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: 

Java




// 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 zoned date time
        ZonedDateTime time = instantObj.atZone(clock.getZone());
 
        // print details of Instant Object
        System.out.println("Instant for class " + clock
                           + " is " + time.toString());
    }
}


Output:

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

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments