Java中的 DayOfWeek minus() 方法及示例
Java .time.DayOfWeek的minus()方法是Java中的一个内置函数,它以一个长整数作为参数,并根据传递的参数指定的向后或向前推进几天后返回 DayOfWeek 的实例。计算从周一到周日在一周结束时滚动。指定的时间段可以是正数或负数。
方法声明:
public DayOfWeek minus(long days)
句法:
DayOfWeek dayOfWeekObject = dayOfWeekObject.minus(long days)
参数:此方法以天为参数,其中:
返回值:该函数在向后或向前推进几天后返回一个 DayOfWeek 的实例。
下面的程序说明了上述方法:
方案一:
// Java Program Demonstrate minus()
// 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.minus(adv);
// Printing the day of the week and its
// Int value before adv days
System.out.println("Day of the Week before "
+ adv + " days: "
+ dayOfWeek.name() + " - "
+ dayOfWeek.getValue());
}
}
输出:
Day of the Week : TUESDAY - 2
Day of the Week before 10 days: SATURDAY - 6
方案二:
// Java Program Demonstrate minus()
// 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.minus(adv);
// Printing the day of the week and its
// Int value before adv days
System.out.println("Day of the Week before "
+ adv + " days: "
+ dayOfWeek.name()
+ " - "
+ dayOfWeek.getValue());
}
}
输出:
Day of the Week : SUNDAY - 7
Day of the Week before -3 days: WEDNESDAY - 3
参考: https: Java/time/DayOfWeek.html#minus-long-