LocalDate类from()方法 (LocalDate Class from() method)
from() method is available in java.time package.
from()方法在java.time包中可用。
from() method is used to create an instance of LocalDate from the given TemporalAccessor object.
from()方法用于根据给定的TemporalAccessor对象创建LocalDate的实例。
from() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
from()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。
from() method may throw an exception at the time of returning LocalDate from the given temp_acc.
从给定的temp_acc返回LocalDate时, from()方法可能会引发异常。
DateTimeException: This exception may throw when the given temp_acc couldn’t convert into LocalDate.
DateTimeException :当给定的temp_acc无法转换为LocalDate时,可能引发此异常。
Syntax:
句法:
public static LocalDate from(TemporalAccessor temp_acc);
Parameter(s):
参数:
TemporalAccessor temp_acc – represents the temporal accessor to convert to LocalDate.
TemporalAccessor temp_acc –表示要转换为LocalDate的时间访问器。
Return value:
返回值:
The return type of this method is LocalDate, it returns the LocalDate that holds the value converted from the given object.
此方法的返回类型为LocalDate ,它返回保存从给定对象转换的值的LocalDate。
Example:
例:
// Java program to demonstrate the example
// of from(TemporalAccessor temp_acc)
// method of LocalDate
import java.time.*;
public class FromOfLocalDate {
public static void main(String args[]) {
// Instantiates a OffsetDateTime
// and a ZonedDateTime object
OffsetDateTime off_da_time = OffsetDateTime.now();
ZonedDateTime zo_da_time = ZonedDateTime.parse("2008-07-06T20:25:30.213+03:00[Africa/Asmara]");
System.out.println();
// Display off_da_time
// and zo_da_time
System.out.println("OffsetDateTime off_da_time: ");
System.out.println("off_da_time: " + off_da_time);
System.out.println();
System.out.println("ZonedDateTime zo_da_time: ");
System.out.println("zo_da_time: " + zo_da_time);
System.out.println();
// Here, this method creates an instance of
// LocalDate from the given temporal
// object OffsetDateTime
LocalDate l_date = LocalDate.from(off_da_time);
//Display l_date
System.out.println("LocalDate.from(off_da_time): " + l_date);
// Here, this method creates an instance of
// LocalDate from the given temporal
// object ZonedDateTime
l_date = LocalDate.from(zo_da_time);
//Display l_date
System.out.println("LocalDate.from(zo_da_time): " + l_date);
}
}
Output
输出量
OffsetDateTime off_da_time:
off_da_time: 2020-05-29T11:41:31.638302Z
ZonedDateTime zo_da_time:
zo_da_time: 2008-07-06T20:25:30.213+03:00[Africa/Asmara]
LocalDate.from(off_da_time): 2020-05-29
LocalDate.from(zo_da_time): 2008-07-06
翻译自: https://www.includehelp.com/java/localdate-from-method-with-example.aspx