Thursday, September 25, 2025
HomeLanguagesJavaInstant toString() method in Java with Examples

Instant toString() method in Java with Examples

The toString() method of Instant class returns string representation of this instant using ISO-8601 representation and format used is the same as DateTimeFormatter.ISO_INSTANT.

Syntax:

public String toString()

Returns: This method returns an ISO-8601 representation of this instant, not null.

Below programs illustrate the toString() method:

Program 1:




// Java program to demonstrate
// Instant.toString() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant
            = Instant.parse("2018-10-28T19:34:50.63Z");
  
        // print Instant using toString()
        System.out.println("Instant: "
                           + instant.toString());
  
        // addition of 84000 seconds to this instant
        Instant returnedValue
            = instant.plusSeconds(84000);
  
        // print result Instant using toString()
        System.out.println("Returned Instant: "
                           + returnedValue.toString());
    }
}


Program 2:




// Java program to demonstrate
// Instant.toString() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant
            = Instant.parse("2022-06-21T19:34:50.63Z");
  
        // print Instant using toString()
        System.out.println("Instant: "
                           + instant);
    }
}


References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#toString()

RELATED ARTICLES

Most Popular

Dominic
32319 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6680 POSTS0 COMMENTS
Nicole Veronica
11853 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6794 POSTS0 COMMENTS
Ted Musemwa
7070 POSTS0 COMMENTS
Thapelo Manthata
6752 POSTS0 COMMENTS
Umr Jansen
6761 POSTS0 COMMENTS