Java ZonedDateTime of()方法及示例

Java ZonedDateTime of()方法及示例

在ZonedDateTime类中,根据传递给它的参数,有三种类型的of()方法。

of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)

ZonedDateTime 类的 of() 方法用于从年、月、日、小时、分钟、秒、纳秒和时区中获得一个ZonedDateTime的实例,所有这些值年、月、日、小时、分钟、秒、纳秒和时区都作为参数传递。

语法

public static ZonedDateTime of(int year, int month, 
                               int dayOfMonth,
                               int hour, int minute,
                               int second, int nanoOfSecond,
                               ZoneId zone)

参数: 该方法接受八个不同的参数。

  • year – 要表示的年份,从MIN_YEAR到MAX_YEAR。
  • month–表示年的月份,从1(1月)到12(12月)。
  • dayOfMonth – 表示每月的日期,从1到31。
  • hour – 代表一天中的小时,从0到23。
  • minute – 代表小时的分钟,从0到59。
  • second–代表分钟的秒,从0到59。
  • nanoOfSecond–代表纳米级的秒,从0到999,999,999。
  • zone – 时区,不为空。

返回值: 该方法返回偏移的日期-时间。

异常: 如果任何字段的值超出了范围,或者月日对月年来说是无效的,这个方法会抛出 DateTimeException

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

程序1 :

// Java program to demonstrate
// ZonedDateTime.of() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ZonedDateTime object
        // using of method
        ZonedDateTime lt
            = ZonedDateTime.of(
                2020, 12, 3, 12, 20, 59,
                90000, ZoneId.systemDefault());
  
        // print result
        System.out.println("ZonedDateTime : "
                           + lt);
    }
}

输出。

ZonedDateTime : 2020-12-03T12:20:59.000090Z[Etc/UTC]

of(LocalDate date, LocalTime time, ZoneId zone)

ZonedDateTime 类的 of(Clock clock) 方法用于从一个本地日期、时间和区域返回一个ZonedDateTime的实例。

语法

public static ZonedDateTime of(LocalDate date,
                               LocalTime time,
                               ZoneId zone)

参数: 该方法接受三个不同的参数。

  • date – 当地日期
  • time – 当地时间
  • zone – 时区

返回值: 该方法返回偏移的日期-时间。

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

程序1 :

// Java program to demonstrate
// ZonedDateTime.of() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDateTime
        LocalDate localdate
            = LocalDate.parse("2020-12-30");
  
        // create a LocalTime
        LocalTime localtime
            = LocalTime.parse("17:52:49");
  
        // create a zoneid
        ZoneId zid
            = ZoneId.of("Europe/Paris");
  
        // create an ZonedDateTime object
        // using of() method
        ZonedDateTime zt
            = ZonedDateTime.of(localdate,
                               localtime, zid);
  
        // print result
        System.out.println("ZonedDateTime : "
                           + zt);
    }
}

输出。

ZonedDateTime : 2020-12-30T17:52:49+01:00[Europe/Paris]

of(LocalDateTime localDateTime, ZoneId zone)

ZonedDateTime 类的 of() 方法用于从localdatetime和zone中返回一个ZonedDateTime的实例,所有这些localdatetime和zone都作为参数传递。

语法

public static ZonedDateTime of(LocalDateTime localDateTime,
                               ZoneId zone)

参数: 该方法接受两个不同的参数。

  • localDateTime – 当地的日期时间
  • time – 当地的时间

返回值: 该方法返回偏移的日期时间。

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

程序1 :

// Java program to demonstrate
// ZonedDateTime.of() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDateTime
        LocalDateTime local
            = LocalDateTime
                  .parse("2018-11-03T12:45:30");
  
        // create a zoneid
        ZoneId zid = ZoneId.of("Europe/Paris");
  
        // create an ZonedDateTime object
        // using of()
        ZonedDateTime zt
            = ZonedDateTime.of(
                local, zid);
  
        // print result
        System.out.println("ZonedDateTime : "
                           + zt);
    }
}

输出。

ZonedDateTime : 2018-11-03T12:45:30+01:00[Europe/Paris]

参考文献:
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#of(int, int, int, int, int, java.time.ZoneId)
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#of(java.time.LocalDate, java.time.LocalTime, java.time.ZoneId)
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#of(java.time.LocalDateTime, java.time.ZoneId)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程