编写一个java应用程序,用户从输入对话框输入两个日期,程

来源:互联网 发布:淘宝综合评分多少分 编辑:程序博客网 时间:2024/05/18 02:49

 

  import java.util.*; 
import javax.swing.JOptionPane;
public class DateExample
{
 public static void main(String []args)
 {
   String str=JOptionPane.showInputDialog("输入第一个日期的年份:");
   int yearOne=Integer.parseInt(str);
   str=JOptionPane.showInputDialog("输入该年的月份:");
       int monthOne=Integer.parseInt(str);
       str=JOptionPane.showInputDialog("输入该年的日期:");
       int dayOne=Integer.parseInt(str);
       
       str=JOptionPane.showInputDialog("输入第二个日期的年份:");
   int yearTwo=Integer.parseInt(str);
   str=JOptionPane.showInputDialog("输入该年的月份:");
       int monthTwo=Integer.parseInt(str);
       str=JOptionPane.showInputDialog("输入该年的日期:");
       int dayTwo=Integer.parseInt(str);
       
       Calendar calendar=Calendar.getInstance();
 calendar.set(yearOne,monthOne,dayOne);
       long timeOne=calendar.getTimeInMillis();
       
       calendar.set(yearTwo,monthTwo,dayTwo);
       long timeTwo=calendar.getTimeInMillis();
       
       Date date1=new Date(timeOne);
       Date date2=new Date(timeTwo); 
       if(date2.equals(date1))
       {System.out.println("两个日期的年、月、日完全相同");}
       else if(date2.after(date1))
       {System.out.println("您输入的第二个日期大于第一个日期");}
       else if(date2.before(date1))
       {System.out.println("您输入的第二个日期小于第一个日期");}
       
       long days=(timeOne-timeTwo)/(1000*60*60*24);
       System.out.println(yearOne+"年"+monthOne+"月"+dayOne+"日和"+yearTwo+"年"+monthTwo+"月"+dayTwo+"相隔"+days+"天");
 }   
 
}
 
原创粉丝点击