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



描述

java.time.LocalDateTime.withMonth(int month) 方法返回一個 LocalDateTime 副本,其中年份中的月份已更改。

宣告

以下是 java.time.LocalDateTime.withMonth(int month) 方法的宣告。

public LocalDateTime withMonth(int month)

public LocalDateTime withMonth(int month)

引數

month - 設定在結果中的年份月份,從 1(1 月)到 12(12 月)。

返回值

基於此日期並具有所請求月份的 LocalDateTime,非空。

異常

DateTimeException - 如果年份中的月份值無效。

示例

package com.tutorialspoint;

import java.time.LocalDateTime;

public class LocalDateTimeDemo {
   public static void main(String[] args) {
      
      LocalDateTime date = LocalDateTime.parse("2017-01-03T10:15:30");
      LocalDateTime result = date.withMonth(3);
      System.out.println(result);  
   }
}

即時演示

2017-03-03T10:15:30
列印頁面
廣告
© . All rights reserved.