java 根据类名示例化类_Java LocalDateTime类| AdjustInto()方法与示例

java 根据类名示例化类

LocalDateTime类AdjustInto()方法 (LocalDateTime Class adjustInto() method)

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

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

  • adjustInto() method is used to adjust this LocalDateTime object into the given Temporal object or in other words we can say the adjusted object is to have the same date and time represented by this object.

    AdjustInto()方法用于将该LocalDateTime对象调整为给定的Temporal对象,换句话说,我们可以说调整后的对象具有与此对象表示的相同日期和时间。

  • adjustInto() 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.

    AdjustInto()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • adjustInto() method may throw an exception at the time of adjusting Date-Time.

    AdjustInto()方法在调整Date-Time时可能会引发异常。

    • DateTimeException – This exception may throw when the given parameter couldn't adjust this LocalDateTime.
    • ArithmeticException – This exception may throw when the calculated result exceeds the length of this Date.

Syntax:

句法:

    public Temporal adjustInto(Temporal temp);

Parameter(s):

参数:

  • Temporal temp – represents the temporal object to adjust this LocalDateTime.

    Temporal temp –表示要调整此LocalDateTime的时间对象。

Return value:

返回值:

The return type of this method is Temporal, it returns the Temporal object that holds the value adjusted this object date-time into the given object.

此方法的返回类型为Temporal ,它将将保存此对象日期时间调整后的值的Temporal对象返回给定对象。

Example:

例:

// Java program to demonstrate the example 
// of adjustInto(Temporal temp) method 
// of LocalDateTime

import java.time.*;

public class AdjustIntoOfLocalDateTime {
    public static void main(String args[]) {
        // Instantiates two LocalDateTime
        // and a ZonedDateTime object
        LocalDateTime da_ti1 = LocalDateTime.parse("2005-10-05T10:10:10");
        LocalDateTime da_ti2 = LocalDateTime.now();
        ZonedDateTime zo_da_time = ZonedDateTime.parse("2008-08-08T20:20:20.213+03:00[Africa/Asmara]");

        // Display da_ti1, da_ti2
        // and zo_da_time
        System.out.println("LocalDateTime da_ti1 and da_ti2: ");
        System.out.println("da_ti1: " + da_ti1);
        System.out.println("da_ti2: " + da_ti2);
        System.out.println();
        System.out.println("ZonedDateTime zo_da_time: ");
        System.out.println("zo_da_time: " + zo_da_time);

        System.out.println();

        // Here, this method adjusts this object
        // into the given object i.e. here we are
        // adjusting this LocalDateTime(da_ti1)
        // into the given object ZonedDateTime(zo_da_time)
        // and the given object is to have same
        // date and time as this object da_ti1
        zo_da_time = (ZonedDateTime) da_ti1.adjustInto(zo_da_time);

        // Display updated zo_da_time
        System.out.println("da_ti1.adjustInto(zo_da_time): " + zo_da_time);

        // Here, this method adjusts this object
        // into the given object i.e. here we are
        // adjusting this LocalDateTime(da_ti2)
        // into the given object ZonedDateTime(zo_da_time)
        // and the given object is to have same
        // date and time as this object da_ti2
        zo_da_time = (ZonedDateTime) da_ti2.adjustInto(zo_da_time);

        // Display updated zo_da_time
        System.out.println("da_ti2.adjustInto(zo_da_time): " + zo_da_time);
    }
}

Output

输出量

LocalDateTime da_ti1 and da_ti2: 
da_ti1: 2005-10-05T10:10:10
da_ti2: 2020-06-04T07:51:48.984048

ZonedDateTime zo_da_time: 
zo_da_time: 2008-08-08T20:20:20.213+03:00[Africa/Asmara]

da_ti1.adjustInto(zo_da_time): 2005-10-05T10:10:10+03:00[Africa/Asmara]
da_ti2.adjustInto(zo_da_time): 2020-06-04T07:51:48.984048+03:00[Africa/Asmara]


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

java 根据类名示例化类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值