Java中用Date类或Calendar类或DateFormat类实现任给两个日期,求它们相差的天数?,给出1990-10-1和2000-3-1相差的天数
时间: 2023-06-04 07:06:47 浏览: 111
您好!根据您的要求,我可以回答您的第一个问题。可以使用两个Date对象相减,获取它们之间的时间差,然后将时间差转换成天数即可。下面是一段示例代码:
```
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public static void main(String[] args) {
String date1 = "1990-10-1";
String date2 = "2000-3-1";
long diff = daysBetween(date1, date2);
System.out.println("相差天数:" + diff);
}
public static long daysBetween(String firstDate, String secondDate){
long diff = 0;
try {
Date d1 = new Date(firstDate);
Date d2 = new Date(secondDate);
//获取时间差,单位为毫秒
diff = d2.getTime() - d1.getTime();
//将时间差转换为天数
diff = diff / (1000 * 60 * 60 * 24);
} catch (Exception e) {
e.printStackTrace();
}
return diff;
}
}
```
运行结果为:相差天数:3422
希望能够帮助到您,如果有其他问题可以继续提问。
阅读全文