Java MonthDay adjustInto()方法及示例
MonthDay 类的 adjustInto() 方法用于调整所传递的时间对象,使其具有此方法所适用的MonthYear。此实例是不可改变的,不受此方法调用的影响。
语法
public Temporal adjustInto(Temporal temporal)
参数: 该方法接受 temporal 作为参数,它是要调整的目标时间对象。它不应该是空的。
返回值: 该方法返回调整后的对象。
异常: 该方法会抛出以下异常。
DateTimeException – 如果不能进行调整。ArithmeticException – 如果不能进行调整。下面的程序说明了adjustInto()方法:
程序1 :
// Java program to demonstrate// MonthDay.adjustInto() method import java.time.*;import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a MonthDay object MonthDay mday = MonthDay.of(10, 31); // print instance System.out.println("MonthDay :" + mday); // create a temporal object LocalDate date = LocalDate.parse("2007-12-03"); // print instance System.out.println("LocalDate :" + date); // apply adjustInto method of Year class LocalDate updatedlocal = (LocalDate)mday.adjustInto(date); // print instance System.out.println("LocalDate after" + " applying adjustInto method: " + updatedlocal); }}
输出。
MonthDay :--10-31LocalDate :2007-12-03LocalDate after applying adjustInto method: 2007-10-31
程序2
// Java program to demonstrate// MonthDay.adjustInto() method import java.time.*;import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a MonthDay object MonthDay mday = MonthDay.of(12, 22); // print instance System.out.println("MonthDay :" + mday); // create a temporal object LocalDate date = LocalDate.parse("2017-12-03"); // print instance System.out.println("LocalDate :" + date); // apply adjustInto method of Year class LocalDate updatedlocal = (LocalDate)mday.adjustInto(date); // print instance System.out.println("LocalDate after" + " applying adjustInto method: " + updatedlocal); }}输出。
MonthDay :--12-22LocalDate :2017-12-03LocalDate after applying adjustInto method: 2017-12-22
**参考资料: ** https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#adjustInto(java.time.temporal.Temporal)。
