java.time.ZonedDateTime.withYear(int year)方法

2019-10-16 22:34:52

java.time.ZonedDateTime.withYear(int year)方法返回年份已更改的ZonedDateTime的副本。

宣告

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

public ZonedDateTime withYear(int year)

引數

  • year - 結果中設定從MIN_YEARMAX_YEAR的年份。

返回值

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

例外

  • DateTimeException - 如果年份值無效。

範例

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

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.withYear(2016);
      System.out.println(result);  
   }
}

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

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