java.time.LocalDateTime.of()方法示例



描述

java.time.LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute)方法從年份、月份、天、小時和分鐘獲取LocalDateTime的一個例項,並將秒和納秒設定為0。

宣告

以下是java.time.LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute)方法的宣告。

public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)

引數

  • 年份 − 要表示的年份,從MIN_YEAR到MAX_YEAR

  • 月份 − 要表示的年份的月份,從1(1月)到12(12月)

  • 當月天數 − 要表示的當月天數,從1到31

  • 小時 − 要表示的時鐘小時數,從0到23

  • 分鐘 − 要表示的小時分鐘數,從0到59

返回值

本地日期時間,非空。

異常

DateTimeException − 如果任何欄位的值超出範圍,或者如果當月天數對於年份月份無效。

示例

以下示例演示瞭如何使用java.time.LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute)方法。

package com.tutorialspoint;

import java.time.LocalDateTime;

public class LocalDateTimeDemo {
   public static void main(String[] args) {
 
      LocalDateTime date = LocalDateTime.of(2017,2,3,6,30);
      System.out.println(date);  
   }
}

讓我們編譯並執行上述程式,它將產生以下結果−

2017-02-03T06:30
廣告
© . All rights reserved.