java.time.OffsetDateTime.with() 方法範例



描述

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

宣告

以下是 java.time.OffsetDateTime.with(TemporalAdjuster adjuster) 方法的宣告。

public OffsetDateTime with(TemporalAdjuster adjuster)

引數

adjuster − 要使用的調整物件,不可為 null。

返回值

基於以下條件的 OffsetDateTime:已進行調整且不為 null。

異常

  • DateTimeException − 如果無法進行調整。

  • ArithmeticException − 如果發生數值溢位。

範例

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

package com.tutorialspoint;

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

public class OffsetDateTimeDemo {
   public static void main(String[] args) {
      
      OffsetDateTime date = OffsetDateTime.parse("2017-01-03T10:15:30+01:00");
      OffsetDateTime result = date.with(Month.JULY).with(TemporalAdjusters.lastDayOfMonth());
      System.out.println(result);  
   }
}

讓我們編譯並執行以上程式,這會產生以下結果 −

2017-07-31T10:15:30+01:00
廣告
© . All rights reserved.