MonthDay類的withDayOfMonth(int dayOfMonth)方法用於將傳遞的dayOfMonth用作參數來更改MonthDay對象的day-of-month,然後該方法返回更改後的MonthDay的副本。如果day-of-month值在指定的月份後無效,則拋出異常改變操作。
用法:
public MonthDay withDayOfMonth(int dayOfMonth)
參數:此方法接受dayOfMonth作為參數,該參數是要在返回的month-day中設置的day-of-month(從1到31)。
返回值:此方法基於此month-day和所請求的日期返回MonthDay。
異常:如果day-of-month值無效,或者當day-of-month在月份中無效,則此方法引發DateTimeException。
以下示例程序旨在說明withDayOfMonth()方法:
示例1:
// Java program to demonstrate
// MonthDay.withDayOfMonth() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create a MonthDay object
MonthDay monthday
= MonthDay.of(8, 28);
// print instance
System.out.println("MonthDay before"
+ " applying method: "
+ monthday);
// apply withDayOfMonth method
// of MonthDay class
MonthDay updatedlocal
= monthday.withDayOfMonth(21);
// print instance
System.out.println("MonthDay after"
+ " applying method: "
+ updatedlocal);
}
}
輸出:
MonthDay before applying method: --08-28 MonthDay after applying method: --08-21
示例2:
// Java program to demonstrate
// MonthDay.withDayOfMonth() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create a MonthDay object
MonthDay monthday
= MonthDay.of(10, 31);
// print instance
System.out.println("MonthDay before"
+ " applying method: "
+ monthday);
// apply withDayOfMonth method
// of MonthDay class
MonthDay updatedlocal
= monthday.withDayOfMonth(12);
// print instance
System.out.println("MonthDay after"
+ " applying method: "
+ updatedlocal);
}
}
輸出:
MonthDay before applying method: --10-31 MonthDay after applying method: --10-12
參考文獻: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#withDayOfMonth(int)
相關用法
- Java LocalDateTime withDayOfMonth()用法及代碼示例
- Java LocalDate withDayOfMonth()用法及代碼示例
- Java OffsetDateTime withDayOfMonth()用法及代碼示例
- Java ZonedDateTime withDayOfMonth()用法及代碼示例
- Java MonthDay from()用法及代碼示例
- Java MonthDay with()用法及代碼示例
- Java MonthDay get()用法及代碼示例
- Java MonthDay atYear()用法及代碼示例
- Java MonthDay getMonthValue()用法及代碼示例
- Java MonthDay getLong()用法及代碼示例
- Java MonthDay isBefore()用法及代碼示例
- Java MonthDay isSupported()用法及代碼示例
- Java MonthDay isValidYear()用法及代碼示例
- Java MonthDay getDayOfMonth()用法及代碼示例
- Java MonthDay getMonth()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 MonthDay withDayOfMonth() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。