java chrono,Java ChronoPeriod addTo()用法及代码示例

使用java.time.chrono包中ChronoPeriod接口的addTo(Temporal)方法将此ChronoPeriod添加到指定的临时对象中,作为参数传递。

用法:

Temporal addTo(Temporal temporalObject)

参数:此方法接受参数TemporalObject,该参数是在此ChronoPeriod中要调整的数量。它不能为空。

返回值:此方法返回一个相同类型的对象,将其TemporalObject调整为该对象。

异常:该方法抛出:

DateTimeException:如果无法添加。

ArithmeticException:如果发生数字溢出。

以下示例说明了ChronoPeriod.addTo()方法:

程序1:

// Java code to show the function addTo()

// to add the two given periods

import java.time.*;

import java.time.chrono.*;

import java.time.temporal.ChronoUnit;

public class ChronoPeriodDemo {

// Driver Code

public static void main(String[] args)

{

// Defining period

int year = 4;

int months = 11;

int days = 10;

ChronoPeriod p1 = Period.of(year, months, days);

// Get the time to be adjusted

LocalDateTime currentTime

= LocalDateTime.now();

// Adjust the time

// using addTo() method

System.out.println(

p1.addTo(currentTime));

}

}

输出:

2024-05-27T14:23:35.242

程序2:

// Java code to show the function addTo()

// to add the two given periods

import java.time.*;

import java.time.chrono.*;

import java.time.temporal.ChronoUnit;

public class ChronoPeriodDemo {

// Driver Code

public static void main(String[] args)

{

// Defining period

int year1 = 2;

int months1 = 7;

int days1 = 8;

ChronoPeriod p1 = Period.of(year1, months1, days1);

// Get the time to be adjusted

LocalDateTime currentTime

= LocalDateTime.now();

// Adjust the time

// using addTo() method

System.out.println(

p1.addTo(currentTime));

}

}

输出:

2022-01-25T14:23:50.411

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值