public class DateTest {
@SneakyThrows
public static void main(String[] args) {
String dchDate = "2022-12-05";
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
Date date = ft.parse(dchDate);//将String类型转为日期
System.out.println(getRQC(date));
}
private static Integer getRQC(Date chrq){
Calendar chCal = Calendar.getInstance();
chCal.setTime(chrq);
Calendar curCal = Calendar.getInstance();//当前日期
curCal.setTime(new Date());
int chDay = chCal.get(Calendar.DAY_OF_YEAR);
int curDay = curCal.get(Calendar.DAY_OF_YEAR);
int chYear = chCal.get(Calendar.YEAR);
int curYear = curCal.get(Calendar.YEAR);
if(chYear != curYear){//不同年
int timeDistance = 0;
for (int i = chYear; i < curYear; i++) {
if(i %4 == 0 && i %100 != 0 || i %400 ==0){//闰年
timeDistance += 366;
}else {//不是闰年
timeDistance += 365;
}
}
return timeDistance + (curDay - chDay);
}else{//同年的话
return curDay-chDay;
}
}
}