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


Java TemporalAdjusters lastDayOfYear()用法及代碼示例

TemporalAdjusters類的lastDayOfYear()方法用於返回“一年中的最後一天” TemporalAdjuster對象,該對象將返回設置為一年中最後一天的新Date對象。

用法:

public static TemporalAdjuster lastDayOfYear()

參數:此方法不接受任何內容。



返回值:此方法返回年份調整器的最後一天,而不是null。

以下示例程序旨在說明TemporalAdjusters.lastDayOfYear()方法:
程序1:

// Java program to demonstrate 
// TemporalAdjusters.lastDayOfYear() 
  
import java.time.LocalDate; 
import java.time.temporal.TemporalAdjuster; 
import java.time.temporal.TemporalAdjusters; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get TemporalAdjuster with last day 
        // of the year adjuster 
        TemporalAdjuster temporalAdjuster 
            = TemporalAdjusters.lastDayOfYear(); 
  
        // using adjuster for local date time 
        LocalDate localDate 
            = LocalDate.of(2023, 10, 11); 
        LocalDate lastDayOfYear 
            = localDate.with(temporalAdjuster); 
  
        // print 
        System.out.println("last day of the "
                           + "year for localdate "
                           + localDate + ":"
                           + lastDayOfYear); 
    } 
}
輸出:
last day of the year for localdate 2023-10-11:2023-12-31

程序2:

// Java program to demonstrate 
// TemporalAdjusters.lastDayOfYear() method 
  
import java.time.LocalDate; 
import java.time.temporal.TemporalAdjuster; 
import java.time.temporal.TemporalAdjusters; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get TemporalAdjuster with last day 
        // of  year adjuster 
        TemporalAdjuster temporalAdjuster 
            = TemporalAdjusters.lastDayOfYear(); 
  
        // using adjuster for local date time 
        LocalDate localDate 
            = LocalDate.of(2099, 12, 29); 
        LocalDate lastDayOfYear 
            = localDate.with(temporalAdjuster); 
  
        // print 
        System.out.println("last day of  "
                           + "year for localdate "
                           + localDate + ":"
                           + lastDayOfYear); 
    } 
}
輸出:
last day of  year for localdate 2099-12-29:2099-12-31

參考文獻:https://docs.oracle.com/javase/10/docs/api/java/time/format/TemporalAdjusters.html




相關用法


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