Java ChronoLocalDateTime minus(TemporalAmount)方法及示例
ChronoLocalDateTime 接口的 minus() 方法用于返回该ChronoLocalDateTime的副本,并将指定的数额减去日期时间。该数额通常是Period或Duration,但也可以是任何其他实现TemporalAmount接口的类型。
语法:
default ChronoLocalDateTime minus(TemporalAmount amountTosubtract)
参数: 该方法接受一个单参数 amountTosubtract ,它是要减去的数量,它不应该是空的。
返回值: 该方法返回基于该日期时间的 ChronoLocalDateTime ,并做了减法,不是空的。
异常:
该方法抛出以下异常。
DateTimeException – 如果不能做减法的话ArithmeticException – 如果发生数字溢出。以下程序说明了minus()方法:
程序1:
// Java program to demonstrate// ChronoLocalDateTime.minus() method import java.time.*;import java.time.chrono.*; public class GFG { public static void main(String[] args) { // Get the ChronoLocalDateTime instance ChronoLocalDateTime ldt = LocalDateTime .parse("2019-12-31T19:15:30"); // Get the String representation // of this ChronoLocalDateTime System.out.println("Original ChronoLocalDateTime: " + ldt.toString()); // subtract 10 Days to ChronoLocalDateTime ChronoLocalDateTime value = ldt.minus(Period.ofDays(10)); // print result System.out.println("ChronoLocalDateTime after" + " subtracting Days: " + value); }}
输出
Original ChronoLocalDateTime: 2019-12-31T19:15:30ChronoLocalDateTime after subtracting Days: 2019-12-21T19:15:30
参考资料: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoLocalDateTime.html#minus-java.time.temporal.TemporalAmount-
