Java中的 DayOfWeek valueOf() 方法及示例
Java .time.DayOfWeek的valueOf()方法是Java中的一个内置函数,它接受一个字符串并返回与该字符串对应的 DayOfWeek 实例。该字符串必须与用于声明此类型中的星期几常量的标识符完全匹配。不允许使用无关的空白字符。
方法声明:
public static DayOfWeek valueOf(String name)
句法:
DayOfWeek dayOfWeekObject = DayOfWeek.valueOf(String name)
参数:此方法以名称作为参数,其中:
返回值:函数返回名称字符串对应的DayOfWeek实例
下面的程序说明了上述方法:
方案一:
// Java Program Demonstrate valueOf()
// method of DayOfWeek
import java.time.*;
class DayOfWeekExample {
public static void main(String[] args)
{
// Initializing a DayOfWeek instance
DayOfWeek dayOfWeek
= DayOfWeek.valueOf("MONDAY");
// Printing the day-of-week
System.out.println(dayOfWeek.name());
}
}
输出:
MONDAY
方案二:
// Java Program Demonstrate valueOf()
// method of DayOfWeek
import java.time.*;
class DayOfWeekExample {
public static void main(String[] args)
{
// Initializing a DayOfWeek instance
DayOfWeek dayOfWeek
= DayOfWeek.valueOf("SATURDAY");
// Printing the day-of-week
System.out.println(dayOfWeek.name());
}
}
输出:
SATURDAY
参考: https: Java/time/DayOfWeek.html#valueOf-java.lang.String-