java 根据类名示例化类
LocalDateTime类atZone()方法 (LocalDateTime Class atZone() method)
atZone() method is available in java.time package.
atZone()方法在java.time包中可用。
atZone() method is used to create a ZonedDateTime by merging this LocalDateTime (date-time) to the given zone.
atZone()方法用于通过将此LocalDateTime(日期时间)合并到给定区域来创建ZonedDateTime。
atZone() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
atZone()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
atZone() method does not throw an exception at the time of representing LocalDateTime.
atZone()方法在表示LocalDateTime时不会引发异常。
Syntax:
句法:
public ZonedDateTime atZone(ZoneId zo);
Parameter(s):
参数:
ZoneId zo – represents the zone to merge with this LocalDateTime.
ZoneId zo –表示要与此LocalDateTime合并的区域。
Return value:
返回值:
The return type of this method is ZonedDateTime, it returns ZonedDateTime object that holds the value merged this LocalDateTime with the given zone.
此方法的返回类型为ZonedDateTime ,它返回ZonedDateTime对象,该对象保存将此LocalDateTime与给定区域合并的值。
Example:
例:
// Java program to demonstrate the example
// of atZone(ZoneId zo) method of LocalDateTime
import java.time.*;
public class AtZoneOfLocalDateTime {
public static void main(String args[]) {
// Instantiates two LocalDateTime
// and a ZonedId
LocalDateTime da_ti1 = LocalDateTime.parse("2005-10-05T10:10:10");
LocalDateTime da_ti2 = LocalDateTime.now();
ZoneId zo = ZoneId.of("Africa/Accra");
// Display da_ti1, da_ti2
// and zo
System.out.println("LocalDateTime da_ti1 and da_ti2: ");
System.out.println("da_ti1: " + da_ti1);
System.out.println("da_ti2: " + da_ti2);
System.out.println();
System.out.println("ZoneId zo: ");
System.out.println("zo: " + zo);
System.out.println();
// Here, this method creates a ZonedDateTime
// by merging this date-time (da_ti1)
// with the given zone (zo) i.e.
// da_ti1 + zo
ZonedDateTime zo_da_time = da_ti1.atZone(zo);
// Display updated zo_da_time
System.out.println("da_ti1.atZone(zo): " + zo_da_time);
// Here, this method creates a ZonedDateTime
// by merging this date-time (da_ti2)
// with the given zone
// (ZoneId.of("Africa/Asmara")) i.e.
// da_ti2 + zone
zo_da_time = da_ti2.atZone(ZoneId.of("Africa/Asmara"));
// Display updated zo_da_time
System.out.println("da_ti2.atZone(ZoneId.of(Africa/Asmara)): " + zo_da_time);
}
}
Output
输出量
LocalDateTime da_ti1 and da_ti2:
da_ti1: 2005-10-05T10:10:10
da_ti2: 2020-06-04T08:10:01.516588
ZoneId zo:
zo: Africa/Accra
da_ti1.atZone(zo): 2005-10-05T10:10:10Z[Africa/Accra]
da_ti2.atZone(ZoneId.of(Africa/Asmara)): 2020-06-04T08:10:01.516588+03:00[Africa/Asmara]
翻译自: https://www.includehelp.com/java/localdatetime-atzone-method-with-example.aspx
java 根据类名示例化类