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