获取年初到当前时间有多少天。
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DaysFromYearStart {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate startOfYear = today.withDayOfYear(1);
long daysFromStart = ChronoUnit.DAYS.between(startOfYear, today);
System.out.println("从年初到当前时间的天数: " + daysFromStart);
}
}
这段代码首先获取了今天的日期,然后将今天的日期调整为年份的开始日期,最后计算出从年初到今天的天数,并打印输出。