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)
相關用法
- Java LocalDate withDayOfYear()用法及代碼示例
- Java OffsetDateTime withDayOfYear()用法及代碼示例
- Java LocalDateTime withDayOfYear()用法及代碼示例
- Java ZonedDateTime with()用法及代碼示例
- Java ZonedDateTime until()用法及代碼示例
- Java ZonedDateTime of()用法及代碼示例
- Java ZonedDateTime get()用法及代碼示例
- Java ZonedDateTime plus()用法及代碼示例
- Java ZonedDateTime now()用法及代碼示例
- Java ZonedDateTime range()用法及代碼示例
- Java ZonedDateTime minusMinutes()用法及代碼示例
- Java ZonedDateTime minusDays()用法及代碼示例
- Java ZonedDateTime plusMonths()用法及代碼示例
- Java ZonedDateTime minusHours()用法及代碼示例
- Java ZonedDateTime withZoneSameInstant()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ZonedDateTime withDayOfYear() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。