Java中的 ZonedDateTime toOffsetDateTime() 方法及示例
ZonedDateTime类的toOffsetDateTime()方法用于将此日期时间转换为 OffsetDateTime,并且忽略区域 ID。
句法:
public OffsetDateTime toOffsetDateTime()
参数:此方法不带任何参数。
返回值:此方法返回一个偏移日期时间,表示相同的本地日期时间和偏移量。
下面的程序说明了 toOffsetDateTime() 方法:
方案一:
// Java program to demonstrate
// ZonedDateTime.toOffsetDateTime() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime zoneddatetime
= ZonedDateTime.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// print result
System.out.println("ZonedDateTime: " + zoneddatetime);
// get OffsetDateTime
OffsetDateTime value = zoneddatetime.toOffsetDateTime();
// print result
System.out.println("OffsetDateTime: " + value);
}
}
输出:
ZonedDateTime: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
OffsetDateTime: 2018-12-06T19:21:12.123+05:30
方案二:
// Java program to demonstrate
// ZonedDateTime.toOffsetDateTime() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime zoneddatetime
= ZonedDateTime.parse(
"2018-10-25T23:12:31.123+02:00[Europe/Paris]");
// print result
System.out.println("ZonedDateTime: " + zoneddatetime);
// get OffsetDateTime
OffsetDateTime value = zoneddatetime.toOffsetDateTime();
// print result
System.out.println("OffsetDateTime: " + value);
}
}
输出:
ZonedDateTime: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
OffsetDateTime: 2018-10-25T23:12:31.123+02:00
参考: https: Java/time/ZonedDateTime.html#toOffsetDateTime()