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


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


Java中OffsetDateTime類的minusWeeks()方法返回此OffsetDateTime的副本,其中包含從解析的日期和時間中減去指定的周數。

用法:

public OffsetDateTime minusWeeks(long weeks)

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


返回值:它基於此日期時間返回OffsetDateTime,其中減去了周,並且不為null。

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

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

示例1:

// Java program to demonstrate the minusWeeks() 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 subtracting weeks: "
                           + date1.minusWeeks(-120)); 
    } 
}
輸出:
Date1: 2018-12-12T13:30:30+05:00
Date1 after subtracting weeks: 2021-03-31T13:30:30+05:00

示例2:

// Java program to demonstrate the minusWeeks() 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 subtracting weeks: "
                           + date1.minusWeeks(140)); 
    } 
}
輸出:
Date1: 2018-12-12T13:30:30+05:00
Date1 after subtracting weeks: 2016-04-06T13:30:30+05:00

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



相關用法


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