LocalDateTime類的plusWeeks()方法用於返回此日期時間的副本,其中添加了指定的星期。
用法:
public LocalDateTime plusWeeks(long weeks)
參數:它接受單個參數week,它指定要添加的周,該周可能為負數。
返回值:此方法基於添加了星期的日期時間返回LocalDateTime。
異常:程序將引發DateTimeException,如果結果超出支持的周數範圍,則將引發DateTimeException。
以下程序說明了Java中的YearMonth.plusWeeks()方法:
程序1:
// 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));
}
}
輸出:
LocalDateTime with 15 weeks added: 2018-04-26T10:15:30
程序2:
// 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://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#plusWeeks(long)
相關用法
- Java ZonedDateTime plusWeeks()用法及代碼示例
- Java LocalDate plusWeeks()用法及代碼示例
- Java OffsetDateTime plusWeeks()用法及代碼示例
- Java LocalDateTime until()用法及代碼示例
- Java LocalDateTime plus()用法及代碼示例
- Java LocalDateTime from()用法及代碼示例
- Java LocalDateTime with()用法及代碼示例
- Java LocalDateTime now()用法及代碼示例
- Java LocalDateTime withSecond()用法及代碼示例
- Java LocalDateTime withYear()用法及代碼示例
- Java LocalDateTime toLocalDate()用法及代碼示例
- Java LocalDateTime getMonth()用法及代碼示例
- Java LocalDateTime toLocalTime()用法及代碼示例
- Java LocalDateTime parse()用法及代碼示例
- Java LocalDateTime toString()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LocalDateTime plusWeeks() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。