Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
java.time.Instant Class in Java
Next article icon

java.time.Instant Class in Java

Last Updated : 09 Mar, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java language, the Instant Class is used to represent the specific time instant on the current timeline. The Instant Class extends the Object Class and implements the Comparable interface.

Declaration of the Instant Class

public final class Instant extends Object      implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable  

Fields of the class:

FieldDescription
EPOCHConstant for the 1970-01-01T00:00:00Z epoch instant.
MAXThe maximum supported Instant, '1000000000-12-31T23:59:59.999999999Z'.
MINThe minimum supported Instant, '-1000000000-01-01T00:00Z'.

Methods of the class:

Method

Description

adjustInto(Temporal temporal)This method adjusts the specified temporal object to have this instant.
atOffset(ZoneOffset offset)This method combines this instant with an offset to create an OffsetDateTime.
atZone(ZoneId zone)This method combines this instant with a time-zone to create a ZonedDateTime.
compareTo(Instant otherInstant)This method compares this instant to the specified instant.
equals(Object otherInstant)This method checks if this instant is equal to the specified instant.
from(TemporalAccessor temporal)This method obtains an instance of Instant from a temporal object.
get(TemporalField field)This method gets the value of the specified field from this instant as an int.
getEpochSecond()This method gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
getLong(TemporalField field)This method gets the value of the specified field from this instant as along.
getNano()This method gets the number of nanoseconds, later along the time-line, from the start of the second.
hashCode()This method returns a hash code for this instant.
isAfter(Instant otherInstant)This method checks if this instant is after the specified instant.
isBefore(Instant otherInstant)This method checks if this instant is before the specified instant.
isSupported(TemporalField field)This method checks if the specified field is supported.
isSupported(TemporalUnit unit)This method checks if the specified unit is supported.
minus(long amountToSubtract, TemporalUnit unit)This method returns a copy of this instant with the specified amount subtracted.
minus(TemporalAmount amountToSubtract)This method returns a copy of this instant with the specified amount subtracted.
minusMillis(long millisToSubtract)This method returns a copy of this instant with the specified duration in milliseconds subtracted.
minusNanos(long nanosToSubtract)This method returns a copy of this instant with the specified duration in nanoseconds subtracted.
minusSeconds(long secondsToSubtract)This method returns a copy of this instant with the specified duration in seconds subtracted.
now()This method obtains the current instant from the system clock.
now(Clock clock)This method obtains the current instant from the specified clock.
ofEpochMilli(long epochMilli)This method obtains an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z.
ofEpochSecond(long epochSecond)This method obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z.
ofEpochSecond(long epochSecond, long nanoAdjustment)This method obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
parse(CharSequence text)This method obtains an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z.
plus(long amountToAdd, TemporalUnit unit)This method returns a copy of this instant with the specified amount added.
plus(TemporalAmount amountToAdd)This method returns a copy of this instant with the specified amount added.
plusMillis(long millisToAdd)This method returns a copy of this instant with the specified duration in milliseconds added.
plusNanos(long nanosToAdd)This method returns a copy of this instant with the specified duration in nanoseconds added.
plusSeconds(long secondsToAdd)This method returns a copy of this instant with the specified duration in seconds added.
query(TemporalQuery<R> query)This method queries this instant using the specified query.
range(TemporalField field)This method gets the range of valid values for the specified field.
toEpochMilli()This method converts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z.
toString()A string representation of this instant using ISO-8601 representation.
truncatedTo(TemporalUnit unit)This method returns a copy of this Instant truncated to the specified unit.
until(Temporal endExclusive, TemporalUnit unit)This method calculates the amount of time until another instant in terms of the specified unit.
with(TemporalAdjuster adjuster)This method returns an adjusted copy of this instant.
with(TemporalField field, long newValue)This method returns a copy of this instant with the specified field set to a new value.

Example:

The following example shows how the different methods associated with the classwork

Java
import java.time.*; import java.time.temporal.*; public class GFG {     public static void main(String[] args)     {         // Parsing a string sequence to Instant         Instant inst1 = Instant.parse("2021-02-09T11:19:42.12Z");         System.out.println("Parsed instant is " + inst1);                // Using isSupported() method to check whether the         // specified field is supported or not         System.out.println(inst1.isSupported(ChronoUnit.DAYS));         System.out.println(inst1.isSupported(ChronoUnit.YEARS));                // Using Instant.now() to get current instant         Instant cur = Instant.now();         System.out.println("Current Instant is " + cur);                // Using minus() method to find instant value after         // subtraction         Instant diff = inst1.minus(Duration.ofDays(70));         System.out.println("Instant after subtraction : "+ diff);                // Using plus() method to find instant value after         // addition         Instant sum = inst1.plus(Duration.ofDays(10));         System.out.println("Instant after addition : "+ sum);     } } 

Output
Parsed instant is 2021-02-09T11:19:42.120Z  true  false  Current Instant is 2021-03-03T16:27:54.378693Z  Instant after subtraction : 2020-12-01T11:19:42.120Z  Instant after addition : 2021-02-19T11:19:42.120Z

Next Article
java.time.Instant Class in Java

A

ashutoshrathi
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2020
  • Java-Instant
  • Java-time package
Practice Tags :
  • Java

Similar Reads

    java.time.InstantSource Class in Java
    The java.time.Instant class in Java basically represents a point in time. It is a precise moment on the timeline of the Unix epoch. The Unix epoch is the time 00:00:00 UTC on 1 January 1970. The Instant class can be used to represent timestamps, durations, and intervals. Instant values can be obtain
    3 min read
    java.time.Duration Class in Java
    Duration is the value-based Class present in the Java time library. It's used to get time-based amount of time. This class is immutable and thread-safe. This article describes all the methods present in this class and some basic examples using class methods.  This class implements Serializable, Comp
    4 min read
    java.time.Clock Class in Java
    Java Clock class is present in java.time package. It was introduced in Java 8 and provides access to current instant, date, and time using a time zone. The use of the Clock class is not mandatory because all date-time classes also have a now() method that uses the system clock in the default time zo
    3 min read
    Java.util.Timer Class in Java
    Timer class provides a method call that is used by a thread to schedule a task, such as running a block of code after some regular instant of time. Each task may be scheduled to run once or for a repeated number of executions. Each timer object is associated with a background thread that is responsi
    6 min read
    java.time.LocalTime Class in Java
    Java is the most popular programming language and widely used programming language. Java is used in all kinds of applications like mobile applications, desktop applications, web applications. As in Java, java.time.LocalTime class represents time, which is viewed as hour-minute-second. This class is
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences