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
相关用法
- Java DayOfWeek plus()用法及代码示例
- Java DayOfWeek of()用法及代码示例
- Java DayOfWeek get()用法及代码示例
- Java DayOfWeek values()用法及代码示例
- Java DayOfWeek adjustInto()用法及代码示例
- Java DayOfWeek getValue()用法及代码示例
- Java WeekFields dayOfWeek()用法及代码示例
- Java DayOfWeek getDisplayName()用法及代码示例
- Java DayOfWeek valueOf()用法及代码示例
- Java DayOfWeek minus()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 DayOfWeek from() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。