時期類的toTotalMonths()方法用於獲取給定時期中的月總數。它返回一個描述它的long值。
用法:
public long toTotalMonths()
參數:此方法不接受任何參數。
返回值:此函數返回long值,該值是此期間內的月總數。
下麵是Period.toTotalMonths()方法的實現:
示例1:
// Java code to demonstrate toTotalMonths() method
import java.time.Period;
class GFG {
public static void main(String[] args)
{
// Get the String to be toTotalMonthsd
String period = "P1Y2M21D";
// Parse the String into Period
Period p = Period.parse(period);
System.out.println("Period: " + p);
// Get the total number of months
// using toTotalMonths() method
System.out.println("Total number of Months"
+ " in this Period: "
+ p.toTotalMonths());
}
}
輸出:
Period: P1Y2M21D Total number of Months in this Period: 14
示例2:
// Java code to demonstrate toTotalMonths() method
import java.time.Period;
class GFG {
public static void main(String[] args)
{
// Get the String to be toTotalMonthsd
String period = "-P1Y2M21D";
// Parse the String into Period
Period p = Period.parse(period);
System.out.println("Period: " + p);
// Get the total number of months
// using toTotalMonths() method
System.out.println("Total number of Months"
+ " in this Period: "
+ p.toTotalMonths());
}
}
輸出:
Period: P-1Y-2M-21D Total number of Months in this Period: -14
參考: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#toTotalMonths–
相關用法
- Java Period plus()用法及代碼示例
- Java Period of()用法及代碼示例
- Java Period between()用法及代碼示例
- Java Period get()用法及代碼示例
- Java Period from()用法及代碼示例
- Java Period minusDays()用法及代碼示例
- Java Period ofDays()用法及代碼示例
- Java Period minus()用法及代碼示例
- Java Period ofMonths()用法及代碼示例
- Java Period minusMonths()用法及代碼示例
- Java Period isZero()用法及代碼示例
- Java Period parse()用法及代碼示例
- Java Period getYears()用法及代碼示例
- Java Period getMonths()用法及代碼示例
- Java Period getDays()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Period toTotalMonths() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。