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


Java ChronoZonedDateTime withEarlierOffsetAtOverlap()用法及代碼示例


ChronoZonedDateTime接口的withEarlierOffsetAtOverlap()方法用於在將區域偏移量更改為本地時間線重疊處的兩個有效偏移量中的較早位置之後,返回此ChronoZonedDateTime對象的副本。當夏時製結束並且將一小時返回到時間線時,就會發生重疊。因此,本地日期時間有兩個有效的偏移量,調用withEarlierOffsetAtOverlap方法將返回帶有兩個區域中較早時間的ChronoZonedDateTime。如果在不重疊的情況下調用此方法,則將返回此方法。此實例是不可變的,不受此方法調用的影響。

用法:

ChronoZonedDateTime withEarlierOffsetAtOverlap()

參數:此方法不接受任何參數。


返回值:此方法基於此日期時間返回ChronoZonedDateTime,並帶有較早的偏移量。

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

示例1:

// Java program to demonstrate 
// withEarlierOffsetAtOverlap() method 
  
import java.time.*; 
import java.time.chrono.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a ChronoZonedDateTime object 
        ChronoZonedDateTime zonedDT 
            = ZonedDateTime 
                  .ofLocal( 
                      LocalDateTime.of(2018, 11, 4, 1, 25, 43), 
                      ZoneId.of("US/Central"), 
                      ZoneOffset.ofHours(-6)); 
  
        // print ChronoZonedDateTime 
        System.out.println("Before"
                           + " withEarlierOffsetAtOverlap():\n "
                           + zonedDT); 
  
        // apply withEarlierOffsetAtOverlap() 
        ChronoZonedDateTime zonedDT2 
            = zonedDT.withEarlierOffsetAtOverlap(); 
  
        // print ChronoZonedDateTime after 
        // withEarlierOffsetAtOverlap() 
        System.out.println("\nAfter"
                           + " withEarlierOffsetAtOverlap():\n "
                           + zonedDT2); 
    } 
}
輸出:
Before withEarlierOffsetAtOverlap():
 2018-11-04T01:25:43-06:00[US/Central]

After withEarlierOffsetAtOverlap():
 2018-11-04T01:25:43-05:00[US/Central]

示例2:

// Java program to demonstrate 
// withEarlierOffsetAtOverlap() method 
  
import java.time.*; 
import java.time.chrono.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a ChronoZonedDateTime object 
        ChronoZonedDateTime zonedDT 
            = ZonedDateTime 
                  .ofLocal( 
                      LocalDateTime.of(2021, 11, 07, 1, 05, 53), 
                      ZoneId.of("US/Central"), 
                      ZoneOffset.ofHours(-6)); 
  
        // print ChronoZonedDateTime 
        System.out.println("Before"
                           + " withEarlierOffsetAtOverlap():\n "
                           + zonedDT); 
  
        // apply withEarlierOffsetAtOverlap() 
        ChronoZonedDateTime zonedDT2 
            = zonedDT.withEarlierOffsetAtOverlap(); 
  
        // print ChronoZonedDateTime after 
        // withEarlierOffsetAtOverlap() 
        System.out.println("\nAfter"
                           + " withEarlierOffsetAtOverlap():\n "
                           + zonedDT2); 
    } 
}
輸出:
Before withEarlierOffsetAtOverlap():
 2021-11-07T01:05:53-06:00[US/Central]

After withEarlierOffsetAtOverlap():
 2021-11-07T01:05:53-05:00[US/Central]

參考: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoZonedDateTime.html#withEarlierOffsetAtOverlap–



相關用法


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