编写一个Java应用程序,用户从输入对话框输入两个日期,程序将判断两个日期的大小关系以及间隔天数

mport java.util.*;
import javax.swing.JOptionPane;
punlic class DateExample
{public static void main(String arg[])
{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);
Canlendar calendar= //初始化日历对象
//将Calendar的时间设置为yearOne年
//monthOne月dayOne日
long timeOne= //将Calendar表示的时间转换成毫秒
//将calendar的时间设置为yearTwo年
//monthTwo月dayTwo日
long timeTwo= //将Calendar表示的时间转换成毫秒
Date date1= //用timeOne作参数构造date1
Date date2= //用timeTwo作参数构造date2
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= //计算两日期相隔天数
System.out.println(yearOne+"年"+monthOne+"月"+"dayOne"日和"+yearTwo+"年"+monthTwo+"月"+dayTwo+"相隔"+days+"天");
}
}

请高手帮帮忙呀,急用!!!!非常感谢!!!!
帮忙补充一下 //前的内容吧,谢谢

public static int getDaysBetween (String beginDate, String endDate) throws ParseException
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date bDate = format.parse(beginDate);
Date eDate = format.parse(endDate);
Calendar d1 = new GregorianCalendar();
d1.setTime(bDate);
Calendar d2 = new GregorianCalendar();
d2.setTime(eDate);
int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
int y2 = d2.get(Calendar.YEAR);
if (d1.get(Calendar.YEAR) != y2)
{
d1 = (Calendar) d1.clone();
do {
days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);//得到当年的实际天数
d1.add(Calendar.YEAR, 1);
} while (d1.get(Calendar.YEAR) != y2);
}
return days;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-01-08
我写给你吧

Calendar calendar= Calendar.getInstance(); //初始化日历对象
calendar.set(Calendar.YEAR, yearOne);//将Calendar的时间设置为yearOne年
calendar.set(Calendar.DATE, monthOne);
calendar.set(Calendar.DATE, dayOne);//monthOne月dayOne日
long timeOne= calendar.getTimeInMillis();//将Calendar表示的时间转换成

calendar.set(Calendar.YEAR, yearTwo);//将calendar的时间设置为yearTwo年
calendar.set(Calendar.DATE, monthTwo);
calendar.set(Calendar.DATE, dayTwo);//monthTwo月dayTwo日
long timeTwo= calendar.getTimeInMillis();//将Calendar表示的时间转换成毫秒
Date date1= new Date(timeOne);//用timeOne作参数构造date1
Date date2= new Date(timeTwo);//用timeTwo作参数构造date2
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= (timeTwo - timeOne) / (long)(24*60*60); //计算两日期相隔天数

最后这里(long)(24*60*60); 记得要cast为long,不然出来的数字很搞笑得本回答被提问者和网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网