使用java.time.chrono包中ChronoPeriod接口的excludeFrom(Temporal)方法將此ChronoPeriod減去指定的時間對象,作為參數傳遞。
用法:
Temporal subtractFrom(Temporal temporalObject)
參數:此方法接受參數TemporalObject,該參數是在此ChronoPeriod中要調整的數量。它不能為空。
返回值:此方法返回一個相同類型的對象,將其TemporalObject調整為該對象。
異常:該方法拋出:
- DateTimeException:如果無法減去。
- ArithmeticException:如果發生數字溢出。
以下示例說明了ChronoPeriod.subtractFrom()方法:
程序1:
// Java code to show the function subtractFrom()
// to subtract 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 subtractFrom() method
System.out.println(
p1.subtractFrom(currentTime));
}
}
輸出:
2014-07-07T14:22:21.929
程序2:
// Java code to show the function subtractFrom()
// to subtract 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 subtractFrom() method
System.out.println(
p1.subtractFrom(currentTime));
}
}
輸出:
2016-11-09T14:22:26.600
相關用法
- Java Duration subtractFrom(Temporal)用法及代碼示例
- Java ChronoPeriod between()用法及代碼示例
- Java ChronoPeriod plus()用法及代碼示例
- Java ChronoPeriod get()用法及代碼示例
- Java ChronoPeriod getUnits()用法及代碼示例
- Java ChronoPeriod negated()用法及代碼示例
- Java ChronoPeriod multipliedBy()用法及代碼示例
- Java ChronoPeriod negated()用法及代碼示例
- Java ChronoPeriod toString()用法及代碼示例
- Java ChronoPeriod equals()用法及代碼示例
- Java ChronoPeriod minus()用法及代碼示例
- Java ChronoPeriod getChronology()用法及代碼示例
- Java ChronoPeriod addTo()用法及代碼示例
- Java ChronoPeriod hashCode()用法及代碼示例
- Java ChronoPeriod isNegative()用法及代碼示例
注:本文由純淨天空篩選整理自srinam大神的英文原創作品 ChronoPeriod subtractFrom() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。