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


Java Year adjustInto()用法及代码示例


Year类的adjustInto()方法用于将传递的时间对象调整为应用此方法的今年,此实例是不可变的,不受此方法调用的影响。

用法:

public Temporal adjustInto(Temporal temporal)

参数:该方法接受时间作为参数,该时间是要调整的目标时间对象。它不能为空。


返回值:此方法返回调整后的对象。

异常:此方法引发以下异常:

  • DateTimeException-如果无法进行调整。
  • ArithmeticException-如果发生数字溢出。

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

// Java program to demonstrate 
// Year.adjustInto() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Year object 
        Year yr 
            = Year.of(2019); 
  
        // create a temporal object 
        LocalDate date = LocalDate.parse("2007-12-03"); 
  
        // print instance 
        System.out.println("LocalDate :"
                           + date); 
  
        // apply adjustInto method of Year class 
        LocalDate updatedlocal = (LocalDate)yr.adjustInto(date); 
  
        // print instance 
        System.out.println("LocalDate after"
                           + " applying adjustInto method: "
                           + updatedlocal); 
    } 
}
输出:
LocalDate :2007-12-03
LocalDate after applying adjustInto method: 2019-12-03

示例2:

// Java program to demonstrate 
// Year.adjustInto() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // create a Year object 
        Year yr 
            = Year.of(2032); 
  
        // create a temporal object 
        LocalDate date = LocalDate.parse("2017-01-13"); 
  
        // print instance 
        System.out.println("LocalDate :"
                           + date); 
  
        // apply adjustInto method of Year class 
        LocalDate updatedlocal = (LocalDate)yr.adjustInto(date); 
  
        // print instance 
        System.out.println("LocalDate after"
                           + " applying adjustInto method: "
                           + updatedlocal); 
    } 
}
输出:
LocalDate :2017-01-13
LocalDate after applying adjustInto method: 2032-01-13

参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/Year.html#adjustInto(java.time.temporal.Temporal)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Year adjustInto() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。