OffsetTime query() method in Java with examples

The Query() method of OffsetTime class in Java queries this time using the specified query.

Syntax :

public  R query(TemporalQuery query)

Parameter: This method accepts a single parameter query which specifies the query to be invoked and not null.



Return Value: It returns the query result, null may be returned (defined by the query).

Errors and Exceptions:
The function throws two excetpions which are described below:

  • DateTimeException: it is thrown if it is unable to query.
  • ArithmeticException: it is thrown if a numeric overflow occurs.
  • Below programs illustrate the query() method:

Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the query() method
  
import java.time.OffsetTime;
import java.time.temporal.TemporalQueries;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Parses the time
        OffsetTime time = OffsetTime.parse("14:25:10+11:00");
  
        System.out.printf("OffsetTime precision is %s%n",
                          time.query(TemporalQueries.precision()));
    }
}

chevron_right


Output:

OffsetTime precision is Nanos

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the query() method
  
import java.time.OffsetTime;
import java.time.temporal.TemporalQueries;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Parses the time
        OffsetTime time = OffsetTime.parse("11:15:20+11:05");
  
        System.out.printf("OffsetTime precision is %s%n",
                          time.query(TemporalQueries.precision()));
    }
}

chevron_right


Output:

OffsetTime precision is Nanos

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#query-java.time.temporal.TemporalQuery-



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.