Javatime-localdatetime-withyear

提供:Dev Guides
移動先:案内検索

java.time.LocalDateTime.withYear()メソッドの例

説明

  • java.time.LocalDateTime.withYear(int year)*メソッドは、年が変更されたこのLocalDateTimeのコピーを返します。

宣言

以下は* java.time.LocalDateTime.withYear(int year)*メソッドの宣言です。

public LocalDateTime withYear(int year)

パラメーター

*year* -結果に設定する年、MIN_YEARからMAX_YEARまで。

戻り値

要求された年を含むこの日付に基づくLocalDateTime、null以外。

例外

*DateTimeException* -年の値が無効な場合。

次の例は、java.time.LocalDateTime.withYear(int year)メソッドの使用法を示しています。

package com.finddevguides;

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

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

2016-01-03T10:15:30