java 根据类名示例化类_Java即时类| 带示例的query()方法

java 根据类名示例化类

即时类query()方法 (Instant Class query() method)

  • query() method is available in java.time package.

    query()方法在java.time包中可用。

  • query() method is used to query this Instant with the help of the given temporal query object.

    query()方法用于在给定的时间查询对象的帮助下查询此Instant。

  • 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()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • query() method may throw an exception at the time of querying this object.

    query()方法在查询该对象时可能会引发异常。

    ArithmeticException: This exception may throw when the calculated result exceeds the limit.

    ArithmeticException :当计算结果超出限制时,可能引发此异常。

Syntax:

句法:

    public Result query(TemporalQuery t_query);

Parameter(s):

参数:

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

    TemporalQuery t_query –表示用于查询该对象的查询对象。

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.

此方法的返回类型为Result ,它返回由给定查询对象定义的查询结果以查询此Instant。

Example:

例:

// 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 = Instant.now();

        // 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);

        System.out.println();

        // 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(TemporalQueries.zone()).toString();

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

Output

输出量

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(TemporalQueries.zone()): Africa/Accra


翻译自: https://www.includehelp.com/java/instant-query-method-with-example.aspx

java 根据类名示例化类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值