ADVERTISEMENT
ADVERTISEMENT

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.