java.time.LocalDateTime.with() 方法示例



描述

java.time.LocalDateTime.with(TemporalField field, long newValue) 方法返回當前日期時間的一個副本,其中指定的欄位設定為新值。

宣告

以下是 java.time.LocalDateTime.with(TemporalField field, long newValue) 方法的宣告。

public LocalDateTime with(TemporalField field, long newValue)

引數

  • field − 結果中要設定的欄位,不能為空。

  • newValue − 結果中欄位的新值。

返回的值

基於此的 LocalDateTime,已進行調整,不能為空。

異常

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

  • UnsupportedTemporalTypeException − 如果不支援該欄位。

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

示例

以下示例演示了 java.time.LocalDateTime.with(TemporalField field, long newValue) 方法的用法。

package com.tutorialspoint;

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;

public class LocalDateTimeDemo {
   public static void main(String[] args) {
      
      LocalDateTime date = LocalDateTime.parse("2017-01-03T10:15:30");
      LocalDateTime result = date.with(ChronoField.DAY_OF_MONTH,13);
      System.out.println(result);  
   }
}

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

2017-01-13T10:15:30
廣告
© . All rights reserved.