Java Instant

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Instant class

Java Instant class is used to represent the specific moment on the time line. It inherits the Object class and implements the Comparable interface.


Java Instant class declaration

Let's see the declaration of java.time.Instant class.

  1. public final class Instant extends Object   

  2. implements Temporal, TemporalAdjuster, Comparable, Serializable  

Methods of Java Instant

Method

Description

Temporal adjustInto(Temporal temporal).

It is used to adjust the specified temporal object to have this instant.

int get(TemporalField field)

It is used to get the value of the specified field from this instant as an int.

boolean isSupported(TemporalField field)

It is used to check if the specified field is supported.

Instant minus(TemporalAmount amountToSubtract)

It is used to return a copy of this instant with the specified amount subtracted.

static Instant now()

It is used to obtain the current instant from the system clock.

static Instant parse(CharSequence text)

It is used to obtain an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z.

Instant plus(TemporalAmount amountToAdd)

It is used to return a copy of this instant with the specified amount added.

Instant with(TemporalAdjuster adjuster)

It is used to return an adjusted copy of this instant.

Java Instant Example: parse()

  1. import java.time.Instant;  
  2. public class InstantExample {  
  3.   public static void main(String[] args) {  
  4.     Instant inst = Instant.parse("2017-02-03T10:37:30.00Z");  
  5.     System.out.println(inst);  
  6.   }  
  7. }  

Output:

2017-02-03T10:37:30Z

Java Instant Example: now()

  1. import java.time.Instant;  
  2. public class InstantExample {  
  3.   public static void main(String[] args) {  
  4.     Instant instant = Instant.now();  
  5.     System.out.println(instant);    
  6.   }  
  7. }  

Output:

2017-02-03T06:11:01.194Z

Java Instant Example: minus()

  1. import java.time.*;  
  2. public class InstantExample {  
  3.   public static void main(String[] args) {  
  4.     Instant instant = Instant.parse("2017-02-03T11:25:30.00Z");  
  5.     instant = instant.minus(Duration.ofDays(125));  
  6.     System.out.println(instant);   
  7.   }  
  8. }  

Output:

2016-10-01T11:25:30Z

Java Instant Example: plus()

  1. import java.time.*;  
  2. public class InstantExample {  
  3.   public static void main(String[] args) {  
  4.     Instant inst1 = Instant.parse("2017-02-03T11:25:30.00Z");  
  5.     Instant inst2 = inst1.plus(Duration.ofDays(125));  
  6.     System.out.println(inst2);   
  7.   }  
  8. }  

Output:

2017-06-08T11:25:30Z

Java Instant Example: isSupported()

  1. import java.time.Instant;  
  2. import java.time.temporal.ChronoUnit;  
  3. public class InstantExample {  
  4.   public static void main(String[] args) {  
  5.     Instant inst = Instant.parse("2017-02-03T11:35:30.00Z");  
  6.     System.out.println(inst.isSupported(ChronoUnit.DAYS));  
  7.     System.out.println(inst.isSupported(ChronoUnit.YEARS));      
  8.   }  
  9. }  

Output:

true
false