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


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


java.time.Month ENUM的adjustInto()方法是Java中的內置函數,該函數接受一個Temporal對象,該對象指定一個日期,並返回與輸入相同的可觀察類型的新Temporal對象,並用此month-of-year替換為月份。

方法聲明

public Temporal adjustInto(Temporal temporal)

用法


Temporal newLocalDate = Month.ANYMONTH.adjustInto(Temporal temporal)

參數:此方法以時間為參數,其中:

  • temporal–是要調整的指定日期。
  • ANYMONTH–是要調整日期的指定月份,例如1月,2月等。
  • newLocalDate–是修改日期。

返回值:該函數返回調整後的Temporal對象,該對象是根據指定的Month調整的日期。

異常

  • DateTimeException:如果無法進行調整,則此方法將引發此異常。
  • ArithmeticException注意:如果發生數字溢出,則拋出此異常。

以下示例程序旨在說明上述方法:

程序1

import java.time.*; 
import java.time.Month; 
import java.time.temporal.Temporal; 
  
class DayOfWeekExample { 
    public static void main(String[] args) 
    { 
        // Set a Local Date whose month is found 
        LocalDate localDate1 
            = LocalDate.of(1947, Month.AUGUST, 15); 
  
        // Find the month from the Local Date 
        Month monthOfYear1 
            = Month.from(localDate1); 
  
        // Printing the Local Date 
        System.out.println(localDate1 
                           + " which is "
                           + monthOfYear1.name()); 
  
        // Adjust the month to JANUARY from AUGUST 
        Temporal localDate2 
            = Month.JANUARY 
                  .adjustInto(localDate1); 
  
        // Find the day from the new Local date 
        Month monthOfYear2 
            = Month.from(localDate2); 
  
        // Printing the new Local Date 
        System.out.println(localDate2 
                           + " which is "
                           + monthOfYear2.name()); 
    } 
}
輸出:
1947-08-15 which is AUGUST
1947-01-15 which is JANUARY

程序2

import java.time.*; 
import java.time.Month; 
import java.time.temporal.Temporal; 
  
class DayOfWeekExample { 
    public static void main(String[] args) 
    { 
        // Set a Local Date whose month is found 
        LocalDate localDate1 
            = LocalDate.of(2019, Month.MARCH, 18); 
  
        // Find the month from the Local Date 
        Month monthOfYear1 
            = Month.from(localDate1); 
  
        // Printing the Local Date 
        System.out.println(localDate1 
                           + " which is "
                           + monthOfYear1.name()); 
  
        // Adjust the month to December from March 
        Temporal localDate2 
            = Month.DECEMBER 
                  .adjustInto(localDate1); 
  
        // Find the day from the new Local date 
        Month monthOfYear2 
            = Month.from(localDate2); 
  
        // Printing the new Local Date 
        System.out.println(localDate2 
                           + " which is "
                           + monthOfYear2.name()); 
    } 
}
輸出:
2019-03-18 which is MARCH
2019-12-18 which is DECEMBER

參考: https://docs.oracle.com/javase/8/docs/api/java/time/Month.html#adjustInto-java.time.temporal.Temporal-



相關用法


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