public class MyDate {
int day;
int month;
int year;
public MyDate(){};
public void function(MyDate d){
//完成这个方法,计算d 和 this 之间的相差天数。
}
}
import java.util.*;
public class MyDate{
int day;
int month;
int year;
public MyDate(){}
public MyDate(int day,int month,int year){
this.day = day;
this.month = month;
this.year = year;
}
public void funtion(MyDate d){
int newDay = d.day;
册凳 int newMonth = d.month;
int newYear = d.year;
Calendar c1 = Calendar.getInstance();
梁卖 c1.set(newYear,newMonth,newDay);
long n1 = c1.getTimeInMillis();
Calendar c2 = Calendar.getInstance();
c2.set(year,month,day);
long n2 = c2.getTimeInMillis();
System.out.println( "相差天数为:" + Math.abs((n1 - n2)/24/3600000)); }
public static void main(String[] args){
MyDate d1 = new MyDate(1,1,2008);
MyDate d2 = 橡姿逗new MyDate(2,1,2001);
d1.funtion(d2);
}
}
import java.util.Calendar;
public class MyDate {
int day;
int month;
int year;
Calendar cal;
public MyDate(int day, int month, int year) {
cal = Calendar.getInstance();
// month从0开始算起,范围为0~11
cal.set(year, month - 1, day);
}
public void function(MyDate d) {
// 完成这个方法,计算d 和 this 之间的相差天数。
Calendar c = (Calendar) d.cal.clone();
int days = 0;
if (cal.after(c)) {
while (cal.after(c)) {
days++;
c.add(Calendar.DATE, 1);
}
} else if (cal.before(c)) {
while (cal.before(c)) {
days++;
樱谨 c.add(Calendar.DATE, -1);
锋颂陆 }
}
System.out.println("相差天数为:" + days);
银顷 }
}