Javatime-localdatetime-adjustInto

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

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

説明

  • java.time.LocalDateTime.adjustInto(Temporal temporal)*メソッドは、指定された時間オブジェクトを調整して、このオブジェクトと同じ日付と時刻を持つようにします。

宣言

以下は、* java.time.LocalDateTime.adjustInto(Temporal temporal)*メソッドの宣言です。

public Temporal adjustInto(Temporal temporal)

パラメーター

*temporal* -調整されるターゲットオブジェクト、nullではない。

戻り値

調整されたオブジェクト、null以外。

例外

  • DateTimeException -調整できない場合。
  • ArithmeticException -数値オーバーフローが発生した場合。

次の例は、java.time.LocalDateTime.adjustInto(Temporal temporal)メソッドの使用方法を示しています。

package com.finddevguides;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;

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

      ZonedDateTime date = ZonedDateTime.now();
      System.out.println(date);

      LocalDateTime date1 = LocalDateTime.parse("2017-02-03T12:30:30");
      date = (ZonedDateTime)date1.adjustInto(date);
      System.out.println(date);
   }
}

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

2017-03-17T12:44:29.823+05:30[Asia/Calcutta]
2017-02-03T12:30:30+05:30[Asia/Calcutta]