Instant parse() method in Java with Examples

The parse() method of Instant class help to get an instance of Instant from a string value passed as parameter. This string is an instant in the UTC time zone. It is parsed using DateTimeFormatter.ISO_INSTANT.

Syntax:

public static Instant 
    parse(CharSequence text)

Parameters: This method accepts one parameter text which is the text to parse in instant. It should not be null.

Returns: This method returns an Instant which is the valid instant in the UTC time xone of the String passed as the parameter.

Exception: This method throws DateTimeException if the text cannot be parsed.



Below programs illustrate the parse() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Instant.parse() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get Instant using parse method
        Instant instant
            = Instant.parse("2018-11-30T18:35:24.00Z");
  
        // print result
        System.out.println("Instant: "
                           + instant);
    }
}

chevron_right


Output:

Instant: 2018-11-30T18:35:24Z

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Instant.parse() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get Instant using parse method
        Instant instant
            = Instant.parse("2019-10-01T08:25:24.00Z");
  
        // print result
        System.out.println("Instant: "
                           + instant);
    }
}

chevron_right


Output:

Instant: 2019-10-01T08:25:24Z

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.