LocalDate类atStartOfDay()方法 (LocalDate Class atStartOfDay() method)
Syntax:
句法:
public LocalDateTime atStartOfDay();
public ZonedDateTime atStartOfDay(ZoneId zo);
atStartOfDay() method is available in java.time package.
atStartOfDay()方法在java.time包中可用。
atStartOfDay() method is used to create a LocalDateTime at the start of this LocalDate by merging this date with the midnight time.
atStartOfDay()方法用于通过将此日期与午夜时间合并在此LocalDate的开始处创建LocalDateTime。
atStartOfDay(ZoneId zo) method is used to create a ZonedDateTime from this LocalDate object at the earliest valid time based on the rules in the given zone.
atStartOfDay(ZoneId zo)方法用于根据给定区域中的规则,在最早的有效时间从此LocalDate对象创建ZonedDateTime。
These methods don’t throw an exception at the time of returning the start of the day.
在返回一天的开始时,这些方法不会引发异常。
These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first cases, atStartOfDay(),
在第一种情况下,在atStartOfDay(),
- None
In the second cases, atStartOfDay(ZoneId zo),
在第二种情况下,在atStartOfDay(ZoneId zo),
- ZoneId zo – represents the zone.
- ZoneId zo –代表区域。
Return value:
返回值:
In the first case, the return type of the method is LocalDateTime, it returns LocalDateTime that holds the value midnight time at the start of this LocalDate object.
在第一种情况下,方法的返回类型为LocalDateTime ,它返回LocalDateTime,该值保存此LocalDate对象开始时的午夜时间值。
In the second case, the return type of the method is ZonedDateTime, it returns ZonedDateTime that holds the value from this date at the earliest time in the given zone.
在第二种情况下,该方法的返回类型为ZonedDateTime ,它返回ZonedDateTime,该值保存了给定区域中最早从该日期开始的值。
Example:
例:
// Java program to demonstrate the example
// of atStartOfDay() method of LocalDate
import java.time.*;
public class AtStartOfDayOfLocalDate {
public static void main(String args[]) {
// Instantiates two LocalDate
LocalDate l_da1 = LocalDate.parse("2007-04-04");
LocalDate l_da2 = LocalDate.now();
// Instantiates a zone for LocalDate l_da2
ZoneId z_id = ZoneId.of("Africa/Accra");
// Display l_da1,l_da2
System.out.println("LocalDate l_da1 and l_da2: ");
System.out.println("l_da1: " + l_da1);
System.out.println("l_da2: " + l_da2);
System.out.println();
// Here, this method creates a LocalDateTime
// by merging this date (l_da1) with
// the time of midnight at the start
// of this date l_da1
LocalDateTime da_ti = l_da1.atStartOfDay();
// Display da_ti
System.out.println("l_da1.l_da1.atStartOfDay(): " + da_ti);
// Here, this method creates a ZonedDateTime
// from this date (l_da2) at the earliest
// the valid time according to the rules
// in the given time-zone (z_id)
ZonedDateTime zo_da_ti = l_da2.atStartOfDay(z_id);
// Display zo_da_ti
System.out.println("l_da2.atStartOfDay(z_id): " + zo_da_ti);
}
}
Output
输出量
LocalDate l_da1 and l_da2:
l_da1: 2007-04-04
l_da2: 2020-06-03
l_da1.l_da1.atStartOfDay(): 2007-04-04T00:00
l_da2.atStartOfDay(z_id): 2020-06-03T00:00Z[Africa/Accra]
翻译自: https://www.includehelp.com/java/localdate-atstartofday-method-with-example.aspx