java.time.InstantSource Class in Java
Last Updated :
08 Apr, 2023
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 obtained from various sources including the clock time, network time, and database time. The Instant.now() is the method that returns the current instant from the system clock. This is useful for obtaining the current timestamp.
Example of obtaining the current timestamp using Instant.now()
Java
/*package whatever //do not write package name here */
import java.time.Instant;
public class InstantExample {
public static void main(String[] args) {
// Get the current instant
Instant now = Instant.now();
System.out.println("Current timestamp: " + now);
}
}
Output:
Current timestamp: 2023-04-03T12:32:34.243715Z
In the addition to the Instant.now(), there are other ways to create Instant objects. For example, you can parse a string that represents an instant using the Instant.parse() method. Here's an example:
Java
/*package whatever //do not write package name here */
import java.time.Instant;
public class InstantExample {
public static void main(String[] args) {
// Parse a string that represents an instant
Instant instant = Instant.parse("2023-04-03T12:32:34.243715Z");
System.out.println("Parsed timestamp: " + instant);
}
}
Output:
Parsed timestamp: 2023-04-03T12:32:34.243715Z
You can also create an Instant object from a java.util.Date object using the Date.toInstant() method. Here's an example:
Java
/*package whatever //do not write package name here */
import java.time.Instant;
import java.util.Date;
public class InstantExample {
public static void main(String[] args) {
// Create a Date object
Date date = new Date();
// Convert the Date object to an Instant
Instant instant = date.toInstant();
System.out.println("Instant from Date: " + instant);
}
}
Output:
Instant from Date: 2023-04-03T12:32:34.244891Z
Instant objects are immutable, meaning that once an Instant object is created, its value not going to be changed. However, you can create a new Instant object based on an existing Instant object by using methods such as Instant.plus() and Instant.minus(). These methods return a new Instant object that is adjusted by the specified amount of time. For example:
Java
/*package whatever //do not write package name here */
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class InstantExample {
public static void main(String[] args) {
// Get the current instant
Instant now = Instant.now();
System.out.println("Current timestamp: " + now);
// Add 1 hour to the current instant
Instant oneHourLater = now.plus(1, ChronoUnit.HOURS);
System.out.println("One hour later: " + oneHourLater);
// Subtract 30 seconds from the current instant
Instant thirtySecondsEarlier = now.minus(30, ChronoUnit.SECONDS);
System.out.println("Thirty seconds earlier: " + thirtySecondsEarlier);
}
}
Output:
Current timestamp: 2023-04-03T12:32:34.245917Z
One hour later: 2023-04-03T13:32:34.
Similar Reads
java.time.Instant Class in Java 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, TemporalAdju
4 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
java.time.LocalDate Class in Java Java is the most popular programming language and widely used programming language. Java is used in all kind of application like as mobile application, desktop application, web application. In this Java java.time.LocalDate class is imported which represents to display the current date. java.time: It
4 min read