当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DayOfWeek values()用法及代码示例


java.time.DayOfWeek的values()方法是Java中的内置函数,它返回包含星期几的数组,例如星期一,星期二等,以它们声明的顺序。

方法声明:

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–



相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 DayOfWeek values() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。