java.time.LocalDateTime.with(TemporalAdjuster adjuster)方法

java.time.LocalDateTime.with(TemporalAdjuster adjuster)方法返回此日期時間的調整副本。

聲明

以下是java.time.LocalDateTime.with(TemporalAdjuster adjuster)方法的聲明。

public LocalDateTime with(TemporalAdjuster adjuster)

參數

adjuster - 要使用的調整器,而不是null

返回值

一個LocalDateTime基於此進行調整,而不是null

異常

  • DateTimeException - 如果無法進行調整。
  • ArithmeticException - 如果發生數字溢出。

示例

以下示例顯示了java.time.LocalDateTime.with(TemporalAdjuster adjuster)方法的用法。

package com.zaixian;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;

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

      LocalDateTime date = LocalDateTime.parse("2017-01-03T10:15:30");
      LocalDateTime result = date.with(Month.JULY).with(TemporalAdjusters.lastDayOfMonth());
      System.out.println(result);
   }
}

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

2017-07-31T10:15:30

上一篇: java.time.LocalDateTime類 下一篇: java.time.LocalTime類