Java LocalDate类| AdjustInto()方法与示例

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

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

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

  • adjustInto() method is used to adjust this LocalDate object into the given Temporal object or in other words we can say the given object holds the same date as this LocalDate object date.

    AdjustInto()方法用于将该LocalDate对象调整为给定的Temporal对象,换句话说,我们可以说给定的对象与该LocalDate对象的日期具有相同的日期。

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

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

    • DateTimeException: This exception may throw when the given parameter couldn't adjust this LocalDate.DateTimeException :如果给定参数无法调整此LocalDate,则可能引发此异常。
    • ArithmeticException: This exception may throw when the calculated result exceeds the length of this Date.ArithmeticException :当计算结果超过此Date的长度时,可能引发此异常。

Syntax:

句法:

    public Temporal adjustInto(Temporal temp);

Parameter(s):

参数:

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

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

Return value:

返回值:

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

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

Example:

例:

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

import java.time.*;

public class AdjustIntoOfLocalDate {
    public static void main(String args[]) {
        // LocalDateiates two LocalDate
        // and a ZonedDateTime object
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.now();
        ZonedDateTime zo_da_time = ZonedDateTime.parse("2008-08-08T20:25:30.213+03:00[Africa/Asmara]");

        // Display l_da1,l_da2
        // and zo_da_time
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);
        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 LocalDate(l_da1) into the
        // given object ZonedDateTime(zo_da_time)
        zo_da_time = (ZonedDateTime) l_da1.adjustInto(zo_da_time);

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

        // Here, this method adjusts this object
        // into the given object i.e. here we are
        // adjusting this LocalDate(l_da2) into the
        // given object ZonedDateTime(zo_da_time)
        zo_da_time = (ZonedDateTime) l_da2.adjustInto(zo_da_time);

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

Output

输出量

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-04
l_da2: 2020-05-29

ZonedDateTime zo_da_time: 
zo_da_time: 2008-08-08T20:25:30.213+03:00[Africa/Asmara]

l_da1.adjustInto(zo_da_time): 2007-04-04T20:25:30.213+03:00[Africa/Asmara]
l_da2.adjustInto(zo_da_time): 2020-05-29T20:25:30.213+03:00[Africa/Asmara]


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值