Java DayOfWeek getValue()方法及示例
java.time.DayOfWeek 的 getValue() 方法是Java中的一个内置函数,用于返回分配给一周7天的整数值,即星期一、星期二、星期三、星期四、星期五、星期六和星期日。该整数值遵循ISO-8601标准,从1(周一)到7(周日)。
方法声明 。
public int getValue()
语法
int val = DayOfWeekObject.getValue()
参数。这个方法不接受任何参数。
返回值: 该函数返回星期几的int值,例如,1代表星期一,2代表星期二,以此类推。
下面的程序说明了上述方法:
程序 1 :
// Java Program Demonstrate getValue()
// method of DayOfWeek
import java.time.*;
import java.time.DayOfWeek;
class DayOfWeekExample {
public static void main(String[] args)
{
// Set a local date whose day is found
LocalDate localDate
= LocalDate.of(1947,
Month.AUGUST, 15);
// Find the day from the local date
DayOfWeek dayOfWeek
= DayOfWeek.from(localDate);
// Printing the day of the week
// and its Int value
System.out.println("Day of the Week on"
+ " 15th August 1947 - "
+ dayOfWeek.name());
int val = dayOfWeek.getValue();
System.out.println("Int Value of "
+ dayOfWeek.name()
+ " - " + val);
}
}
输出:
Day of the Week on 15th August 1947 - FRIDAY
Int Value of FRIDAY - 5
程序2
// Java Program Demonstrate getValue()
// method of DayOfWeek
import java.time.*;
import java.time.DayOfWeek;
class DayOfWeekExample {
public static void main(String[] args)
{
// Set a local date whose day is found
LocalDate localDate
= LocalDate.of(2015,
Month.JULY, 13);
// Find the day from the local date
DayOfWeek dayOfWeek
= DayOfWeek.from(localDate);
// Printing the day of the week
// and its Int value
System.out.println("Day of the Week on"
+ " 13th July 2015 - "
+ dayOfWeek.name());
int val = dayOfWeek.getValue();
System.out.println("Int Value of "
+ dayOfWeek.name()
+ " - " + val);
}
}
输出:
Day of the Week on 13th July 2015 - MONDAY
Int Value of MONDAY - 1
参考资料: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#getValue-