當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java MonthDay adjustInto()用法及代碼示例


MonthDay類的adjustInto()方法用於將傳遞的時間對象調整為具有應用此方法的MonthYear。此實例是不可變的,不受此方法調用的影響。

用法:

public Temporal adjustInto(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-31
LocalDate :2007-12-03
LocalDate 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-22
LocalDate :2017-12-03
LocalDate after applying adjustInto method: 2017-12-22

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#adjustInto(java.time.temporal.Temporal)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 MonthDay adjustInto() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。