Java 中的 ZoneOffset systemDefault()方法,示例

原文:https://www . geesforgeks . org/zone offset-system default-method-in-Java-with-examples/

Java 中 ZoneOffset 类的 systemDefault() 方法用于返回系统默认时区。

语法:

public String systemDefault()

参数:该方法不接受任何参数。

返回值:该方法返回系统默认时区。

异常:该方法抛出以下异常:

  • Date-time exception –If the converted area identification format is invalid, this exception will be thrown.
  • Area rule exception –If the converted area ID cannot be found, this exception will be thrown.

以下程序说明了 systemDefault()方法:

程序 1:

// Java program to demonstrate
// ZoneOffset.systemDefault() method

import java.time.*;

public class GFG {
    public static void main(String[] args)
    {

        // create ZoneId object
        ZoneId zoneId
            = ZoneId.systemDefault();

        // printresult
        System.out.println("System Default time-zone: "
                           + zoneId);
    }
}

输出:

System Default time-zone: Etc/UTC

程序二:

// Java program to demonstrate
// ZoneOffset.systemDefault() method

import java.time.*;

public class GFG {
    public static void main(String[] args)
    {

        // create ZoneId object
        ZoneId zoneId
            = ZoneId.systemDefault();

        if (zoneId.getId().equals("Etc/UTC"))
            System.out.println("This zone is Etc/UTC");
        else
            System.out.println("This zone is not Etc/UTC");
    }
}

输出:

This zone is Etc/UTC

参考: 甲骨文文档