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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。