当前位置: 首页>>代码示例>>用法及示例精选>>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。