当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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