📌  相关文章
📜  Java中的 LocalDateTime getDayOfWeek() 方法及示例

📅  最后修改于: 2022-05-13 01:55:38.549000             🧑  作者: Mango

Java中的 LocalDateTime getDayOfWeek() 方法及示例

LocalDateTime 类的getDayOfWeek()方法用于返回星期几字段,这是一个枚举 DayOfWeek。此方法返回星期几的枚举 DayOfWeek。

句法:

public DayOfWeek getDayOfWeek()

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

返回:此方法从枚举 DayOfWeek 返回星期几

下面的程序说明了 LocalDateTime.getDayOfWeek() 方法:

方案一:

// 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

方案二:

// 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: Java/time/LocalDateTime.html#getDayOfWeek()