Home »
Java »
Java Reference »
Java Instant Class
Java Instant Class | parse() Method with Example
Instant Class parse() method: Here, we are going to learn about the parse() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 26, 2020
Instant Class parse() method
- parse() method is available in java.time package.
- parse() method is used to get an Instant that parses the given char sequence and char sequence follow some standard format like 2007-12-03T10:15:30.00Z.
- parse() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
- parse() method may throw an exception at the time of parsing the given sequence.
DateTimeParseException: This exception may throw when the given parameter couldn't parse.
Syntax:
public static Instant parse(CharSequence cs);
Parameter(s):
- CharSequence cs – represents the char sequence to parse to a Instant.
Return value:
The return type of this method is Instant, it returns the Instant that holds the parse value of the given sequence to an Instant.
Example:
// Java program to demonstrate the example
// of parse(CharSequence cs) method
// of Instant
import java.time.*;
public class ParseOfInstant {
public static void main(String args[]) {
CharSequence c_seq = "2006-04-04T10:30:30.60Z";
// Here, this method creates an Instant
// from a parse CharSequence
Instant ins = Instant.parse(c_seq);
// Display ins
System.out.println("Instant.parse(c_seq): " + ins);
// Here, this method creates an Instant
// from a parse string
ins = Instant.parse("2008-01-01T10:10:10.00Z");
// Display ins
System.out.println("Instant.parse(2008-01-01T10:10:10.00Z): " + ins);
}
}
Output
Instant.parse(c_seq): 2006-04-04T10:30:30.600Z
Instant.parse(2008-01-01T10:10:10.00Z): 2008-01-01T10:10:10Z