当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java 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

相关用法


注:本文由纯净天空筛选整理自 java.time.LocalDateTime.withSecond() Method Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。