Java LocalDate Class | query() Method with Example

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

LocalDate Class query() method

  • query() method is available in java.time package.
  • query() method is used to query this LocalDate 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.
    • DateTimeException: This exception may throw when it couldn’t query this object.
    • ArithmeticException: This exception may throw when the calculated result exceeds the limit.

Syntax:

    public Result query(TemporalQuery t_query);

Parameter(s):

  • 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.

Example:

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

import java.time.*;
import java.time.temporal.*;

public class QueryOfLocalDate {
    public static void main(String args[]) {
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.of(2008, Month.FEBRUARY, 06);

        // Create a zone for LocalDate l_da2
        ZoneId z_id = ZoneId.of("Africa/Accra");
        ZonedDateTime zone = l_da2.atStartOfDay(z_id);

        // Create an offset for
        // LocalDate l_da1
        OffsetTime off = OffsetTime.now();
        OffsetDateTime da_with_off = l_da1.atTime(off);

        // Display l_da1,l_da2 
        System.out.println("LocalDate l_da1,l_da2 : ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);

        System.out.println();

        // Here, this method queries to this 
        // LocalDate l_da1 with the help of 
        // the given query i.e. here we are
        // querying to this l_da1 for
        // precision
        String query = l_da1.query(TemporalQueries.precision()).toString();

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

        // Here, this method queries to this 
        // OffsetDateTime (da_with_off) with
        // the help of the given query i.e.
        // here we are querying to this da_with_off for
        // offset
        query = da_with_off.query(TemporalQueries.offset()).toString();

        // Display query
        System.out.println("da_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(TemporalQueries.zone()).toString();

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

Output

LocalDate l_da1,l_da2 : 
l_da1: 2007-04-04
l_da2: 2008-02-06

l_da1.query(TemporalQueries.precision()): Days
da_with_off.query(TemporalQueries.offset()): Z
zone.query(TemporalQueries.zone()): Africa/Accra


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.