當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。