當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java OffsetDateTime plusWeeks()用法及代碼示例


Java中OffsetDateTime類的plusWeeks()方法返回此OffsetDateTime的副本,其中將指定的星期數添加到解析的日期和時間。

用法:

public OffsetDateTime plusWeeks(long weeks)

參數:此方法接受單個參數week,該參數指定要添加到解析日期的星期。也可以是負數,在這種情況下,它減去周數。


返回值:它基於此日期時間返回OffsetDateTime,其中添加了星期,但不為null。

異常:程序超出支持的數據和時間範圍時,將引發DateTimeException。

以下示例程序旨在說明plusWeeks()方法:

示例1:

// Java program to demonstrate the plusWeeks() method 
  
import java.time.OffsetDateTime; 
import java.time.ZonedDateTime; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Parses the date1 
        OffsetDateTime date1 
            = OffsetDateTime 
                  .parse( 
                      "2018-12-12T13:30:30+05:00"); 
  
        // Prints dates 
        System.out.println("Date1: " + date1); 
  
        // Subtracts the number of weeks 
        System.out.println("Date1 after adding weeks: "
                           + date1.plusWeeks(-120)); 
    } 
}
輸出:
Date1: 2018-12-12T13:30:30+05:00
Date1 after adding weeks: 2016-08-24T13:30:30+05:00

程序2

// Java program to demonstrate the plusWeeks() method 
  
import java.time.OffsetDateTime; 
import java.time.ZonedDateTime; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Parses the date1 
        OffsetDateTime date1 
            = OffsetDateTime 
                  .parse( 
                      "2018-12-12T13:30:30+05:00"); 
  
        // Prints dates 
        System.out.println("Date1: " + date1); 
  
        // Subtracts the number of weeks 
        System.out.println("Date1 after adding weeks: "
                           + date1.plusWeeks(140)); 
    } 
}
輸出:
Date1: 2018-12-12T13:30:30+05:00
Date1 after adding weeks: 2021-08-18T13:30:30+05:00

參考: https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#plusWeeks(long)



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 OffsetDateTime plusWeeks() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。