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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.