用java写一个程序可以计算2个日期之间的天数差.
来源:学生作业帮 编辑:搜狗做题网作业帮 分类:综合作业 时间:2024/08/19 05:04:27
用java写一个程序可以计算2个日期之间的天数差.
public class MyDate {
int day;
int month;
int year;
public MyDate(){};
public void function(MyDate d){
//完成这个方法,计算d 和 this 之间的相差天数.
}
}
public class MyDate {
int day;
int month;
int year;
public MyDate(){};
public void function(MyDate d){
//完成这个方法,计算d 和 this 之间的相差天数.
}
}
![用java写一个程序可以计算2个日期之间的天数差.](/uploads/image/z/3886821-45-1.jpg?t=%E7%94%A8java%E5%86%99%E4%B8%80%E4%B8%AA%E7%A8%8B%E5%BA%8F%E5%8F%AF%E4%BB%A5%E8%AE%A1%E7%AE%972%E4%B8%AA%E6%97%A5%E6%9C%9F%E4%B9%8B%E9%97%B4%E7%9A%84%E5%A4%A9%E6%95%B0%E5%B7%AE.)
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);
}
}
再问: 我能 有你的扣 _ 扣么。
再答: 采纳问题了我就私信你~
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);
}
}
再问: 我能 有你的扣 _ 扣么。
再答: 采纳问题了我就私信你~