Java中的YearMonth類的atEndOfMonth()方法用於基於與其一起使用的Yearyearth對象返回月份的最後一天的LocalDate。
用法:
public LocalDate atEndOfMonth()
參數:此方法不接受任何參數。
返回值:它返回帶有本YearMonth對象指定的當月最後一天的本地日期。此方法不返回NUll值。
以下程序說明了Java中YearMonth的atEndOfMonth()方法:
示例1::
// Programt to illustrate the atEndOfMonth() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Creates a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2017, 8);
// Creates a local date with this
// YearMonth object passed to it
// Last day of this month is 31
LocalDate date = thisYearMonth.atEndOfMonth();
System.out.println(date);
}
}
輸出:
2017-08-31
示例2::此方法還可以照顧Le年。
// Programt to illustrate the atEndOfMonth() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Creates a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2016, 2);
// Creates a local date with this
// YearMonth object passed to it
// Last day of February is
// 29 as 2016 is a leap year
LocalDate date = thisYearMonth.atEndOfMonth();
System.out.println(date);
}
}
輸出:
2016-02-29
參考: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#atEndOfMonth-
相關用法
- Java YearMonth equals()用法及代碼示例
- Java YearMonth getMonth()用法及代碼示例
- Java YearMonth format()用法及代碼示例
- Java YearMonth compareTo()用法及代碼示例
- Java YearMonth with()用法及代碼示例
- Java YearMonth until()用法及代碼示例
- Java YearMonth getMonthValue()用法及代碼示例
- Java YearMonth atDay()用法及代碼示例
- Java YearMonth hashCode()用法及代碼示例
- Java YearMonth getYear()用法及代碼示例
- Java YearMonth isValidDay()用法及代碼示例
- Java YearMonth range()用法及代碼示例
- Java YearMonth plus(TemporalAmount)用法及代碼示例
- Java YearMonth lengthOfYear()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 YearMonth atEndOfMonth() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。