Java Instant is a set of pre-defined methods in Java, which can be used in the code blocks for performing specific functions in the program. A few of the most commonly used Java Instant methods are toString(), parse(), ofEpochMilli(), getEpochSecond(), getNano(), plusMillis(long milliToAdd), plusNanos(long nanosToAdd), plusSeconds(long secondsToAdd), minusSeconds(long secondsToSubtract), minusMillis(long millisToSubtract), minusNanos(long nanosToSubtract), compareTo(Instant otherInstant), isAfter(Instant otherInstant), and isBefore(Instant otherInstant). This feature in Java is known for its higher efficiency, remarkable performance, reusability, optimized code length, etc.
The Instant class provides multiple static factory methods to create the Instance of Instant class. The following are the static methods by which we can get Instance of Instant class with different standard values.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Instant instant = Instant.now();
The method now() returns the current instant from the system clock. This instance represents the total number of nanoseconds from the start time or EPOCH. This is the time counted from the beginning of the 1st Jan 1970. This method to get the instant is useful for representing machine timestamp and will be used more frequently than others.
Instant instant = Instant.EPOCH;
This static field will return an Instance of Instant class representing the exact EPOCH time.
Instant instant = Instant.MAX;
This static field will return an Instance of Instant class representing the maximum value of the instance.
Instant instant = Instant.MIN;
This static field will return an Instance of Instant class representing the minimum value of instance, and the value is negative.
So here comes the main part or usage of Instant class. Following is the list of some of the main methods which belong to the Instant class.
Below are the methods which can be used to get an instance of Instant class.
The instance of Instant class represents time with nanoseconds precision as compared to util.Date ( milliseconds precision ). Therefore, the instant requires more storage to store its value (larger than long). So, the instant class stores the second’s value in a long variable and the remaining nanoseconds value in the int variable. We can access these separate values using the below methods.
Below are the methods which can be used in instant manipulation or calculations.
Similar methods exist for minus or subtraction operation. These methods will subtract the number of seconds passed from the instant and will return a new instant.
Below are the methods which can be used to compare the two instants,
Below are the examples of implementing java instant:
Code:
import java.time.Instant; public class app { public static void main(String[] args) { Instant instant = Instant.now(); System.out.println( instant.toString() ); } }
Output:
The output will be the time of the system running the code.
Code:
import java.time.Instant; public class app { public static void main(String[] args) { Instant instant = Instant.now(); System.out.println( instant.getEpochSecond() ); } }
Output:
Code:
import java.time.Instant; public class app { public static void main(String[] args) { Instant instant = Instant.now(); System.out.println( instant.getNano() ); } }
Output:
Code:
import java.time.Instant; public class app { public static void main(String[] args) { Instant instant = Instant.now(); System.out.println( instant ); instant = instant.plusSeconds( 3600 ); System.out.println( instant ); } }
Output:
We have added exactly 3600 seconds, i.e. 1 hour, to the current instant, as it can be seen in the output that time is increased by 1 hour.
Code:
import java.time.Instant; public class app { public static void main(String[] args) { Instant instant = Instant.now(); System.out.println( instant ); Instant instant2 = instant.plusSeconds( 3600 ); System.out.println( instant.compareTo( instant2 ) ); } }
Output:
Here, we are comparing two instants, current with the instant having added 1 more hour. As the current instance is less than instance2, the output is negative.
So, we have seen the Instant class introduced from Java 8. This class represents the seconds from the start of the timeline with nanoseconds precision. This class is useful to generate the timestamp, which will represent the system time. We have seen different types of instants which we can get and different methods useful for calculations, creating a new instantly, comparing instants, getting seconds and nanoseconds differently, etc. Instant class is immutable and provides thread safety as compared to the old Date object. The Instant class is much more powerful than the old time-date API.
The above is the detailed content of Java Instant. For more information, please follow other related articles on the PHP Chinese website!