Java DayOfWeek values()方法及示例
java.time.DayOfWeek 的 values() 方法是Java中的一个内置函数,它返回一个包含一周内各天的数组,如MONDAY、TUESDAY等,按照它们被声明的顺序。
方法声明 。
public static DayOfWeek[] values()
语法
DayOfWeek week[] = DayOfWeek.values()
参数。此方法不接受任何参数。
返回值: 该函数返回一个数组,该数组包含了按声明顺序排列的一周的日子。
以下程序说明了上述方法:
程序1 :
// 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
程序2
// 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://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#values-