The java.time.Instant
class represents a specific moment on the time-line in UTC (Coordinated Universal Time). It is a machine-readable timestamp and is commonly used for recording timestamps in logs, measuring time intervals, or working with system clocks.
- It is immutable and thread-safe.
- The epoch reference point is 1970-01-01T00:00:00Z (UTC).
Commonly Used Methods of Instant

Simple Program Using Instant
import java.time.Instant; import java.time.Duration; public class SimpleInstantExample { public static void main(String[] args) { Instant start = Instant.now(); // Simulate some processing time for (int i = 0; i < 1000000; i++); Instant end = Instant.now(); Duration timeElapsed = Duration.between(start, end); System.out.println("Start time: " + start); System.out.println("End time: " + end); System.out.println("Elapsed time in milliseconds: " + timeElapsed.toMillis()); } }
Problem Statement
Paani and Mahesh are developers at BankTech Inc. tasked with building a Secure Transaction Logger. Each time a transaction is completed, they must record:
- The timestamp of the transaction.
- The processing duration.
- Whether the transaction completed before or after a specific business timestamp.
They must use the Instant
class to implement this and determine if the processing met the SLA (Service Level Agreement).
import java.time.Instant; import java.time.Duration; public class TransactionLogger { public static void main(String[] args) { // Reference timestamp for comparison (e.g., 2025-05-22T10:00:00Z) Instant slaDeadline = Instant.parse("2025-05-22T10:00:00Z"); // Simulate transaction start Instant transactionStart = Instant.now(); System.out.println("Transaction started at: " + transactionStart); // Simulate processing delay simulateTransactionProcessing(); // Simulate transaction end Instant transactionEnd = Instant.now(); System.out.println("Transaction ended at: " + transactionEnd); // Calculate processing time Duration duration = Duration.between(transactionStart, transactionEnd); System.out.println("Transaction processing time: " + duration.toMillis() + " ms"); // Check SLA compliance if (transactionEnd.isBefore(slaDeadline)) { System.out.println("Transaction completed before SLA deadline."); } else { System.out.println("Transaction missed the SLA deadline."); } } private static void simulateTransactionProcessing() { try { Thread.sleep(500); // Simulating transaction delay } catch (InterruptedException e) { e.printStackTrace(); } } } /* Transaction started at: 2025-05-22T09:59:59.123Z Transaction ended at: 2025-05-22T10:00:00.623Z Transaction processing time: 1500 ms Transaction completed before SLA deadline. */
The Instant
class in the java.time
package is a precise, efficient, and immutable representation of a single moment on the UTC timeline. It is particularly useful for:
- Timestamping events (like transactions, log entries).
- Measuring elapsed time between two events.
- Comparing time instants to enforce SLAs or deadlines.
- System-level or backend operations where human-readable dates are not necessary.
With its machine-centric nature, Instant
complements human-readable classes like LocalDateTime
and ZonedDateTime
, offering robust tools for developers handling both real-world and technical time-tracking needs. Its thread safety, immutability, and clean API make it a preferred choice for modern Java applications.