import java.text.ParseException;
import java.text.SimpleDateFormat;
//日期类
public class Date {
private String year;
private String month;
private String day;
public Date(String year, String month, String day) {
this.year = year;
this.month = month;
this.day = day;
}
public void format(){
System.out.println(day + "/" + month + "/" + year);
}
public void calculate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date startDate = sdf.parse(year + "/" + "01" + "/" + "01");
java.util.Date inputDate = sdf.parse(year + "/" + month + "/" + day);
long resultDay = (inputDate.getTime() - startDate.getTime())/(24 * 1000 * 60 * 60);
System.out.println("第" + (resultDay + 1) + "天");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
//测试类
public class Test {
public static void main(String[] args) {
Date date1 = new Date("2020","04","11");
Date date2 = new Date("2020","01","02");
date1.format();
date1.calculate();
date2.format();
date2.calculate();
}
}
温馨提示:内容为网友见解,仅供参考