Java Instant Class | query() Method with Example

Instant Class query() method: Here, we are going to learn about the query() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 28, 2020

Instant Class query() method

  • query() method is available in java.time package.
  • query() method is used to query this Instant with the help of the given temporal query object.
  • query() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • query() method may throw an exception at the time of querying this object.
    ArithmeticException: This exception may throw when the calculated result exceeds the limit.


    public Result query(TemporalQuery t_query);


  • TemporalQuery t_query – represents the query object by which to query this object.

Return value:

The return type of this method is Result, it returns the queried result that is defined by the given query object to query this Instant.


// Java program to demonstrate the example 
// of Result query(TemporalQuery t_query) method 
// of Instant

import java.time.*;

import java.time.temporal.*;
public class QueryOfInstant {
    public static void main(String args[]) {
        // Instantiates two Instant
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.60Z");
        Instant ins2 =;

        // Create a zone offset for
        // Instant ins2
        ZoneOffset z_off = ZoneOffset.ofHours(3);
        OffsetDateTime ins_with_off = ins2.atOffset(z_off);

        // Create a zone for Instant ins2
        ZoneId z_id = ZoneId.of("Africa/Accra");
        ZonedDateTime zone = ins2.atZone(z_id);

        // Display ins1,ins2 and seconds
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);


        // Here, this method queries this Instant ins1
        // with the help of the given query i.e.
        // here we are querying to this ins1 for
        // precision

        String query = ins1.query(TemporalQueries.precision()).toString();

        // Display query
        System.out.println("ins1.query(TemporalQueries.precision()): " + query);

        // Here, this method queries to this ins_with_off
        // with the help of the given query i.e.
        // here we are querying to this ins_with_off for
        // offset

        query = ins_with_off.query(TemporalQueries.offset()).toString();

        // Display query
        System.out.println("ins_with_off.query(TemporalQueries.offset()): " + query);

        // Here, this method queries to this zone
        // with the help of the given query i.e.
        // here we are querying to this zone for
        // zone

        query = zone.query(;

        // Display query
        System.out.println("zone.query( " + query);


Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15.600Z
ins2: 2020-05-27T05:15:09.456314Z

ins1.query(TemporalQueries.precision()): Nanos
ins_with_off.query(TemporalQueries.offset()): +03:00
zone.query( Africa/Accra


Comments and Discussions



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

© some rights reserved.