1
import java.util.*;
2
3
import javax.swing.JOptionPane;
4
5
public class test
6

{
7
public static void main(String args[ ])
8
{
9
String str=JOptionPane.showInputDialog("输入第一个日期的年份:");
10
int yearOne=Integer.parseInt(str);
11
str=JOptionPane.showInputDialog("输入该年的月份:");
12
int monthOne=Integer.parseInt(str);
13
str=JOptionPane.showInputDialog("输入该月份的日期:");
14
int dayOne=Integer.parseInt(str);
15
str=JOptionPane.showInputDialog("输入第二个日期的年份:");
16
int yearTwo=Integer.parseInt(str);
17
str=JOptionPane.showInputDialog("输入该年的月份:");
18
int monthTwo=Integer.parseInt(str);
19
str=JOptionPane.showInputDialog("输入该月份的日期:");
20
int dayTwo=Integer.parseInt(str);
21
Calendar calendar=Calendar.getInstance(); //初始化日历对象
22
calendar.set(yearOne, monthOne, dayOne); //将calendar的时间设置为yearOne年monthOne月dayOne日
23
long timeOne= calendar.getTimeInMillis() ; //calendar表示的时间转换成毫秒
24
calendar.set(yearTwo, monthTwo, dayTwo); //将calendar的时间设置为yearTwo年monthTwo月dayTwo日
25
long timeTwo= calendar.getTimeInMillis(); //calendar表示的时间转换成毫秒。
26
Date date1=new Date(timeOne); // 用timeOne做参数构造date1
27
Date date2=new Date(timeTwo); // 用timeTwo做参数构造date2
28
if(date2.equals(date1))
29
{
30
System.out.println("两个日期的年、月、日完全相同");
31
}
32
else if(date2.after(date1))
33
{
34
System.out.println("您输入的第二个日期大于第一个日期");
35
}
36
else if(date2.before(date1))
37
{
38
System.out.println("您输入的第二个日期小于第一个日期");
39
}
40
long days=Math.abs((timeOne-timeTwo)/1000/60/60/24);;//计算两个日期相隔天数
41
System.out.println(yearOne+"年"+monthOne+"月"+dayOne+"日和"
42
+yearTwo+"年"+monthTwo+"月"+dayTwo+"相隔"+days+"天");
43
}
44
}