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


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


java.time.DayOfWeek的from()方法是Java中的内置函数,该函数采用TemporalAccessor定义日期,并返回与该日期对应的DayOfWeek实例。 TemporalAccessor代表日期和时间信息的任意集合,该方法将其转换为与该日期相对应的DayOfWeek实例。

方法声明:

 public static DayOfWeek from(TemporalAccessor temporal)

用法:


 DayOfWeek dayOfWeekObject = DayOfWeek.from(TemporalAccessor temporal)

参数:此方法将时间作为参数,其中:

  • temporal – 是一个TemporalAccessor,它代表一个日期。
  • dayOfWeekObject – 是DayOfWeek的实例。

返回值:该函数返回DayOfWeek的一个实例,该实例与由temporal指定的日期相对应

以下示例程序旨在说明上述方法:
示例1:

// Java Program Demonstrate from() 
// 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(1997, Month.AUGUST, 15); 
  
        // Initialize a DayOfWeek object 
        // with specified local Date 
        DayOfWeek dayOfWeek 
            = DayOfWeek.from(localDate); 
  
        // Printing the day of the week 
        System.out.println("Day of the Week on "
                           + localDate + " - "
                           + dayOfWeek.name()); 
    } 
}
输出:
Day of the Week on 1997-08-15 - FRIDAY

示例2:

// Java Program Demonstrate from() 
// method of DayOfWeek 
  
import java.time.*; 
  
class DayOfWeekExample { 
    public static void main(String[] args) 
    { 
        // Set a local date whose day is found 
        LocalDate localDate 
            = LocalDate.of(2015, Month.JULY, 13); 
  
        // Initialize a DayOfWeek object 
        // with specified local Date 
        DayOfWeek dayOfWeek 
            = DayOfWeek.from(localDate); 
  
        // Printing the day of the week 
        System.out.println("Day of the Week on "
                           + localDate + " - "
                           + dayOfWeek.name()); 
    } 
}
输出:
Day of the Week on 2015-07-13 - MONDAY

参考: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#from-java.time.temporal.TemporalAccessor-



相关用法


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