题目来源:
题目描述
输入一个"YYYY-MM-dd"格式的日期字符串,输出该天是当年的第几天(1 月 1 日是每年的第 1 天)
题目解析:
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String dest=sc.nextLine();
LocalDate localDateNow=LocalDate.parse(dest);//根据字符串获取当前日期
LocalDate localDateOne=LocalDate.parse(localDateNow.getYear()+"-01-01");
int i=(int) ChronoUnit.DAYS.between(localDateOne,localDateNow);//计算时间差
System.out.println(i+1);
}
}
1、LocalDate:用来处理日期的类
头文件:
java.time.LocalDate ->只对年月日做出处理
java.time.LocalTime ->只对时分秒纳秒做出处理
java.time.LocalDateTime ->同时可以处理年月日和时分秒
2、ChronoUnit类可用于在单个时间单位内测量一段时间,例如天数或秒。
可以使用between()方法来查找两个日期之间的区别。