java.time.LocalDateTime.withYear(int year)
方法返回此LocalDateTime
的副本,其中年份已更改。
声明
以下是java.time.LocalDateTime.withYear(int year)
方法的声明。
public LocalDateTime withYear(int year)
参数
year
- 结果中设置的年份,从MIN_YEAR
到MAX_YEAR
。
返回值
基于此日期的LocalDateTime
与请求的年份,不为null
。
异常
DateTimeException
- 如果年份值无效。
示例
以下示例显示了java.time.LocalDateTime.withYear(int year)
方法的用法。
package com.zaixian;
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.withYear(2016);
System.out.println(result);
}
}
编译并运行上面的程序,这将产生以下结果 -
2016-01-03T10:15:30
上一篇:
java.time.LocalDateTime类
下一篇:
java.time.LocalTime类