Java中的 DayOfWeek from() 方法及示例
Java .time.DayOfWeek的from()方法是Java中的一个内置函数,它接受定义日期的 TemporalAccessor 并返回对应于该日期的 DayOfWeek 实例。 TemporalAccessor 表示任意日期和时间信息集,此方法将其转换为对应于该日期的 DayOfWeek 实例。
方法声明:
public static DayOfWeek from(TemporalAccessor temporal)
句法:
DayOfWeek dayOfWeekObject = DayOfWeek.from(TemporalAccessor temporal)
参数:此方法将时间作为参数,其中:
返回值:该函数返回一个 DayOfWeek 实例,对应temporal指定的日期
下面的程序说明了上述方法:
方案一:
// 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
方案二:
// 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: Java/time/DayOfWeek.html#from-java.time.temporal.TemporalAccessor-