Java中的 DayOfWeek values() 方法及示例
Java .time.DayOfWeek的values()方法是Java中的一个内置函数,它按照声明的顺序返回一个包含星期几的数组,例如MONDAY、TUESDAY 等。
方法声明:
public static DayOfWeek[] values()
句法:
DayOfWeek week[] = DayOfWeek.values()
参数:此方法不接受任何参数。
返回值:该函数返回一个数组,其中包含按声明顺序排列的星期几。
下面的程序说明了上述方法:
方案一:
// Java Program Demonstrate values()
// method of DayOfWeek
import java.time.DayOfWeek;
class DayOfWeekExample {
public static void main(String[] args)
{
// Initializing an array containing
// all the days of the Week
DayOfWeek week[] = DayOfWeek.values();
// Printing the days of the Week
for (DayOfWeek c : week)
System.out.println(c);
}
}
输出:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
方案二:
// Java Program Demonstrate values()
// method of DayOfWeek
import java.time.DayOfWeek;
class DayOfWeekExample {
public static void main(String[] args)
{
// Initializing an array containing
// all the days of the Week
DayOfWeek week[] = DayOfWeek.values();
// Printing the days of the Week
for (int i = 0; i < week.length; i++)
System.out.println(week[i]
+ " has int value - "
+ (i + 1));
}
}
输出:
MONDAY has int value - 1
TUESDAY has int value - 2
WEDNESDAY has int value - 3
THURSDAY has int value - 4
FRIDAY has int value - 5
SATURDAY has int value - 6
SUNDAY has int value - 7
参考: https: Java/time/DayOfWeek.html#values–