1 package ex6_1;
2
3 import java.sql.Date;
4 import java.util.Calendar;
5
6 import javax.swing.JOptionPane;
7
8 public class DateExample {
9 public static void main(String[] args) {
10 String str=JOptionPane.showInputDialog("输入第一个日期的年份"
);
11 int yearOne=
Integer.parseInt(str);
12 str=JOptionPane.showInputDialog("输入该年的月份"
);
13 int monthOne=
Integer.parseInt(str);
14 str=JOptionPane.showInputDialog("输入该月份的日期"
);
15 int dayOne=
Integer.parseInt(str);
16 str=JOptionPane.showInputDialog("输入第二个日期的年份"
);
17 int yearTwo=
Integer.parseInt(str);
18 str=JOptionPane.showInputDialog("输入该年的月份"
);
19 int monthTwo=
Integer.parseInt(str);
20 str=JOptionPane.showInputDialog("输入该月份的日期"
);
21 int dayTwo=
Integer.parseInt(str);
22
23 Calendar cal=
Calendar.getInstance();
24 cal.set(yearOne, monthOne, dayOne);
25 long timeOne=
cal.getTimeInMillis();
26 cal.set(yearTwo, monthTwo, dayTwo);
27 long timeTwo=
cal.getTimeInMillis();
28 Date date1=
new Date(timeOne);
29 Date date2=
new Date(timeTwo);
30 if(date2.equals(date1)) {
31 System.out.println("两个日期的年月日完全相同"
);
32 }
33 else if (date2.after(date1)) {
34 System.out.println("第二个日期小于第一个日期"
);
35 }
36 else if (date2.before(date1)) {
37 System.out.println("第二个日期大于第一个日期"
);
38 }
39 long days=Math.abs(timeOne-timeTwo)/(3600*24*1000
);
40 System.out.println(yearOne+"年"+monthOne+"月"+dayOne+"日和"
41 +yearTwo+"年"+monthTwo+"月"+dayTwo+"相隔"+days+"天"
);
42 }
43 }
转载于:https://www.cnblogs.com/kazama/p/10181076.html
相关资源:比较两个日期的大小和间隔天数