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


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