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


Java ZonedDateTime withDayOfYear()用法及代码示例


ZonedDateTime类的withDayOfYear()方法用于更改此ZonedDateTime中的day-of-year,并在此操作后返回ZonedDateTime的副本。此方法在本地时间线上操作,更改本地日期时间的day-of-year,并在此操作后更改本地日期时间使用区域ID获取偏移量,将其转换回ZonedDateTime。当转换回ZonedDateTime时,如果本地日期时间重叠,则将尽可能保留偏移量,否则将使用较早的偏移量。此实例是不可变的,不受此方法调用的影响。

用法:

public ZonedDateTime withDayOfYear(int dayOfYear)

参数:此方法接受单个参数dayOfYear,该参数表示要在结果中设置的day-of-year,范围为1至365-366。


返回值:此方法基于此日期时间和所请求的日期返回ZonedDateTime。

异常:如果day-of-year值无效,或者day-of-year对于year-year无效,则此方法引发DateTimeException。

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

// Java program to demonstrate 
// ZonedDateTime.withDayOfYear() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a ZonedDateTime object 
        ZonedDateTime zoneddatetime 
            = ZonedDateTime.parse( 
                "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]"); 
  
        // print instance 
        System.out.println("ZonedDateTime before"
                           + " alter dayOfYear: "
                           + zoneddatetime); 
  
        // alter dayOfYear to 200th day 
        ZonedDateTime returnvalue 
            = zoneddatetime.withDayOfYear(200); 
  
        // print result 
        System.out.println("ZonedDateTime after "
                           + "alter dayOfYear: "
                           + returnvalue); 
    } 
}
输出:

ZonedDateTime before alter dayOfYear: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after alter dayOfYear: 2018-07-19T19:21:12.123+05:30[Asia/Calcutta]

示例2:

// Java program to demonstrate 
// ZonedDateTime.withDayOfYear() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a ZonedDateTime object 
        ZonedDateTime zoneddatetime 
            = ZonedDateTime.parse( 
                "2018-10-25T23:12:31.123+02:00[Europe/Paris]"); 
  
        // print instance 
        System.out.println("ZonedDateTime before"
                           + " alter dayOfYear: "
                           + zoneddatetime); 
  
        // alter dayOfYear to 365 
        ZonedDateTime returnvalue 
            = zoneddatetime.withDayOfYear(365); 
  
        // print result 
        System.out.println("ZonedDateTime after "
                           + "alter dayOfYear: "
                           + returnvalue); 
    } 
}
输出:

ZonedDateTime before alter dayOfYear: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
ZonedDateTime after alter dayOfYear: 2018-12-31T23:12:31.123+01:00[Europe/Paris]

参考: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#withDayOfYear(int)



相关用法


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