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


Java LocalTime adjustInto()用法及代碼示例


LocalTime類的adjustInto()方法用於將指定的時間對象調整為與此LocatTime對象相同的時間。

用法:

public Temporal adjustInto(Temporal temporal)

參數:此方法接受單個參數temporal,它是要調整的目標對象,並且不具體為null。


返回值:此方法返回調整後的時間對象。

異常:該函數引發兩個異常,如下所述:

  • DateTimeException–如果無法進行調整
  • ArithmeticException–如果發生數字溢出

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

示例1:

// Java program to demonstrate 
// LocalTime.adjustInto() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime Object 
        LocalTime local 
            = LocalTime.parse("09:32:42"); 
  
        // create Zone Time 
        ZonedDateTime zonetime = ZonedDateTime.now(); 
  
        // print Zone Time 
        System.out.println("ZonedDateTime"
                           + " before adjustInto():"
                           + zonetime.toString()); 
  
        // apply adjustInto() 
        zonetime = (ZonedDateTime)local 
                       .adjustInto(zonetime); 
  
        // print Zone Time 
        System.out.println("ZonedDateTime"
                           + " after adjustInto():"
                           + zonetime.toString()); 
    } 
}
輸出:
ZonedDateTime before adjustInto():2018-12-03T06:30:31.142Z[Etc/UTC]
ZonedDateTime after adjustInto():2018-12-03T09:32:42Z[Etc/UTC]

示例2:

// Java program to demonstrate 
// LocalTime.adjustInto() method 
  
import java.time.*; 
import java.time.temporal.Temporal; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime Object 
        LocalTime local = LocalTime.parse("19:52:43"); 
  
        // create a Temporal object 
        // which is equal to OffsetDateTime object 
        OffsetDateTime passTemporal 
            = OffsetDateTime.now(); 
  
        // print passed Value 
        System.out.println("Before adjustInto() OffsetDateTime: "
                           + passTemporal); 
  
        // apply adjustInto method 
        // to adjust OffsetDateTime Temporal 
        Temporal returnTemporal 
            = local.adjustInto(passTemporal); 
  
        // print results 
        System.out.println("After adjustInto() OffsetDateTime: "
                           + (OffsetDateTime)returnTemporal); 
    } 
}
輸出:
Before adjustInto() OffsetDateTime: 2018-12-03T06:30:33.927Z
After adjustInto() OffsetDateTime: 2018-12-03T19:52:43Z

參考: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#adjustInto(java.time.temporal.Temporal)



相關用法


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