LocalDateTime withSecond(int second)
描述 (Description)
java.time.LocalDateTime.withSecond(int seconds)方法返回此LocalDateTime的副本,并更改了秒数。
声明 (Declaration)
以下是java.time.LocalDateTime.withSecond(int seconds)方法的声明。
public LocalDateTime withSecond(int seconds)
参数 (Parameters)
seconds - 在结果中设置的秒数,从0到59。
返回值 (Return Value)
一个LocalDateTime基于此日期与请求的秒数,而不是null。
异常 (Exceptions)
DateTimeException - 如果秒值无效。
例子 (Example)
以下示例显示了java.time.LocalDateTime.withSecond(int seconds)方法的用法。
package com.iowiki;
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