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.