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



說明

java.time.LocalDateTime.withSecond(int seconds) 方法返回該 LocalDateTime 的副本,其中秒進行了更改。

宣告

以下是對 java.time.LocalDateTime.withSecond(int seconds) 方法的宣告。

public LocalDateTime withSecond(int seconds)

引數

seconds - 在結果中要設定的秒數,從 0 至 59。

返回值

基於此日期且具有請求的秒數的 LocalDateTime,非空。

異常

DateTimeException - 如果秒值無效。

示例

以下示例展示了 java.time.LocalDateTime.withSecond(int seconds) 方法的用法。

package com.tutorialspoint;

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

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

2017-03-03T10:15:40
廣告
© . All rights reserved.