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


Java TemporalAdjusters firstDayOfYear()用法及代码示例


TemporalAdjusters类的firstDayOfYear()方法用于返回“一年的第一天” TemporalAdjuster对象,该对象返回设置为该年第一天的新的Date对象。

用法:

public static TemporalAdjuster firstDayOfYear()

参数:此方法不接受任何内容。



返回值:此方法返回年份调节器的第一天,而不是null。

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

// Java program to demonstrate 
// TemporalAdjusters.firstDayOfYear() 
  
import java.time.LocalDate; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get TemporalAdjuster with first day 
        // of the year adjuster 
        TemporalAdjuster temporalAdjuster 
            = TemporalAdjusters.firstDayOfYear(); 
  
        // using adjuster for local date-time 
        LocalDate localDate 
            = LocalDate.of(2012, 05, 31); 
        LocalDate firstDayOfYear 
            = localDate.with(temporalAdjuster); 
  
        // print 
        System.out.println("First day of the "
                           + "year for localdate "
                           + localDate + ":"
                           + firstDayOfYear); 
    } 
}
输出:
First day of the year for localdate 2012-05-31:2012-01-01

程序2:

// Java program to demonstrate 
// TemporalAdjusters.firstDayOfYear() method 
  
import java.time.LocalDate; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get TemporalAdjuster with first day 
        // of the year adjuster 
        TemporalAdjuster temporalAdjuster 
            = TemporalAdjusters.firstDayOfYear(); 
  
        // using adjuster for local date time 
        LocalDate localDate 
            = LocalDate.of(2000, 12, 21); 
        LocalDate firstDayOfYear 
            = localDate.with(temporalAdjuster); 
  
        // print 
        System.out.println("First day of the "
                           + "year for localdate "
                           + localDate + ":"
                           + firstDayOfYear); 
    } 
}
输出:
First day of the year for localdate 2000-12-21:2000-01-01

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




相关用法


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