Java中的 LocalDateTime plusWeeks() 方法及示例
LocalDateTime 类的plusWeeks()方法用于返回此日期时间的副本,其中添加了指定的周数。
语法:
public LocalDateTime plusWeeks(long weeks)
参数:它接受单个参数周,指定要添加的周数,可能为负数。
返回值:此方法基于此日期时间返回一个LocalDateTime并添加周数。
异常:如果结果超出支持的周数范围,程序会抛出DateTimeException 。
下面的程序说明了Java中的 LocalDateTime.plusWeeks() 方法:
程序 1 :
Java
// Program to illustrate the plusWeeks() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
LocalDateTime dt1
= LocalDateTime
.parse("2018-01-11T10:15:30");
System.out.println("LocalDateTime with 15 weeks added: "
+ dt1.plusWeeks(15));
}
}
Java
// Program to illustrate the plusWeeks() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
LocalDateTime dt1
= LocalDateTime
.parse("2018-01-11T08:15:30");
System.out.println("LocalDateTime with -2 weeks added: "
+ dt1.plusWeeks(-2));
}
}
输出:
LocalDateTime with 15 weeks added: 2018-04-26T10:15:30
方案二:
Java
// Program to illustrate the plusWeeks() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
LocalDateTime dt1
= LocalDateTime
.parse("2018-01-11T08:15:30");
System.out.println("LocalDateTime with -2 weeks added: "
+ dt1.plusWeeks(-2));
}
}
输出:
LocalDateTime with -2 weeks added: 2017-12-28T08:15:30
参考:https: Java/time/LocalDateTime.html#plusWeeks(long)