Java DayOfWeek plus()方法及示例
java.time.DayOfWeek 的 plus() 方法是Java中的一个内置函数,它接收一个长整数作为参数,并按照所传递的参数指定的时间向前或向后推进一些日子后,返回DayOfWeek的一个实例。该计算围绕周末从周日到周一进行滚动。指定的周期可以是正数或负数。
方法声明 。
public DayOfWeek plus(long days)
语法
DayOfWeek dayOfWeekObject = dayOfWeekObject.plus(long days)
参数: 该方法将天数作为参数,其中。
- days – 是要向前或向后推进的天数。
- dayOfWeekObject – 是DayOfWeek对象的一个实例。
返回值: 该函数在向前或向后推进若干天后返回DayOfWeek的实例。
以下程序说明了上述方法:
程序 1 :
// 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
程序2
// 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://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#plus-long-