java.time.ZonedDateTime.withMonth(int month)方法

2019-10-16 22:34:48

java.time.ZonedDateTime.withMonth(int month)方法返回此ZonedDateTime其中年份已更改的副本。

宣告

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

public ZonedDateTime withMonth(int month)

引數

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

返回值

基於此日期與請求月份的ZonedDateTime,而不是null

例外

  • DateTimeException - 如果年月值無效。

範例

以下範例顯示了java.time.ZonedDateTime.withMonth(int month)方法的用法。

package com.yiibai;

import java.time.ZonedDateTime;

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

      ZonedDateTime date = ZonedDateTime.parse("2017-03-28T12:25:38.492+05:30[Asia/Calcutta]");
      ZonedDateTime result = date.withMonth(3);
      System.out.println(result);  
   }
}

編譯並執行上面的程式,這將產生以下結果 -

2017-03-28T12:25:38.492+05:30[Asia/Calcutta]