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


Java LocalDateTime getDayOfWeek()用法及代码示例


LocalDateTime类的getDayOfWeek()方法用于返回day-of-week字段,该字段是枚举DayOfWeek。此方法返回day-of-week的枚举DayOfWeek。

用法:

public DayOfWeek getDayOfWeek()

参数:此方法不接受任何参数。


返回值:此方法从枚举DayOfWeek返回day-of-week。

以下示例程序旨在说明LocalDateTime.getDayOfWeek()方法:

示例1:

// Java program to demonstrate 
// LocalDateTime.getDayOfWeek() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalDateTime Object 
        LocalDateTime local 
            = LocalDateTime.parse("2018-12-03T12:39:10"); 
  
        // get dayofweek for this LocalDateTime 
        DayOfWeek dayofweek 
            = local.getDayOfWeek(); 
  
        // print result 
        System.out.println("Day of Week: "
                           + dayofweek); 
    } 
}
输出:
Day of Week: MONDAY

示例2:

// Java program to demonstrate 
// LocalDateTime.getDayOfWeek() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalDateTime Object 
        LocalDateTime local 
            = LocalDateTime.parse("2018-12-29T02:39:10"); 
  
        // get dayofweek for this LocalDateTime 
        DayOfWeek dayofweek = local.getDayOfWeek(); 
  
        // print result 
        System.out.println("Day of Week: "
                           + dayofweek); 
    } 
}
输出:
Day of Week: SATURDAY

参考:https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#getDayOfWeek()



相关用法


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