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


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


java.time.DayOfWeek的adjustInto()方法是Java中的內置函數,該方法接受指定日期的Temporal對象,並返回與輸入相同的可觀察類型的新Temporal對象,其中day-of-week更改為與指定的DayOfWeek相同不變。請注意,此方法在星期一至星期日的星期內向前或向後調整。

方法聲明:

 public Temporal adjustInto(Temporal temporal)

用法:


 Temporal newLocalDate = DayOfWeek.ANYWEEKDAY.adjustInto(Temporal temporal)

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

  • temporal – 是要調整的指定日期。
  • ANYWEEKDAY – 是要調整日期的指定日期,例如MONDAY,TUESDAY等。
  • newLocalDate – 是修改日期。

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

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

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

示例2:

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

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



相關用法


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