📜  Java中的 DayOfWeek plus() 方法及示例

📅  最后修改于: 2022-05-13 01:55:24.028000             🧑  作者: Mango

Java中的 DayOfWeek plus() 方法及示例

Java .time.DayOfWeekplus()方法是Java中的一个内置函数,它以一个长整数作为参数,并根据传递的参数指定的向前或向后推进几天后返回 DayOfWeek 的实例。计算从周日到周一在一周结束时滚动。指定的时间段可以是正数或负数。

方法声明:

public DayOfWeek plus(long days)

句法:

DayOfWeek dayOfWeekObject = dayOfWeekObject.plus(long days)

参数:此方法以为参数,其中:

  • - 是前进或后退的天数。
  • dayOfWeekObject – 是 DayOfWeek 对象的一个实例。

    返回值:该函数在向前或向后推进几天后返回一个 DayOfWeek 的实例。

    下面的程序说明了上述方法:
    方案一:

    // Java Program Demonstrate plus()
    // method of DayOfWeek
    import java.time.DayOfWeek;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Getting an instance of DayOfWeek from int value
            DayOfWeek dayOfWeek = DayOfWeek.of(2);
      
            // Printing the day of the week
            // and its Int value
            System.out.println("Day of the Week : "
                               + dayOfWeek.name()
                               + " - "
                               + dayOfWeek.getValue());
      
            // Number of days to advance
            long adv = 10;
      
            // Advancing the day
            dayOfWeek = dayOfWeek.plus(adv);
      
            // Printing the day of the week and its
            // Int value after adv days
            System.out.println("Day of the Week after "
                               + adv + " days: "
                               + dayOfWeek.name() + " - "
                               + dayOfWeek.getValue());
        }
    }
    
    输出:
    Day of the Week : TUESDAY - 2
    Day of the Week after 10 days: FRIDAY - 5
    

    方案二:

    // Java Program Demonstrate plus()
    // method of DayOfWeek
    import java.time.DayOfWeek;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Getting an instance of DayOfWeek
            // from int value
            DayOfWeek dayOfWeek = DayOfWeek.of(7);
      
            // Printing the day of the week
            // and its Int value
            System.out.println("Day of the Week : "
                               + dayOfWeek.name()
                               + " - "
                               + dayOfWeek.getValue());
      
            // Number of days to advance
            long adv = -3;
      
            // Advancing the day
            dayOfWeek = dayOfWeek.plus(adv);
      
            // Printing the day of the week and its
            // Int value after adv days
            System.out.println("Day of the Week after "
                               + adv + " days: "
                               + dayOfWeek.name()
                               + " - "
                               + dayOfWeek.getValue());
        }
    }
    
    输出:
    Day of the Week : SUNDAY - 7
    Day of the Week after -3 days: THURSDAY - 4
    

    参考: https: Java/time/DayOfWeek.html#plus-long-