您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 薪酬管理 > java实验2(计算日期源代码)
1实验二简单数据类型和流程控制4:设intyear,month,day分别表示一个日期中的年月日,试编程求a)对于任意三个整数,判断其是否为一个合法的日期值。b)给定一个日期值,计算若干天后的日期值c)如果有两个日期值,计算他们相距的天数。实验原理:先了解日期,年份分为平年闰年,其中平年365天,这年的二月份为28天,闰年有366天,这年二月份有29天,月份除了二月之外,30天和31天之分,这主要用到数组来进行定义月份的天数,利用Scanner类从键盘中输入日期和相隔天数。输入日期不能超出那个日历相应的规定值实验过程:需引用的:Scanner包名:date类:Datetext属性:year,month,day;方法:intDatetrue(intyear,intmonth,intday)(这个方法功能是验证输入的日期是否正确,其中包括月份,每月的天数的验证,具体详细,看函数标注)intCountDate(intyear1,intmonth1,intday1,intcount1)(给定一个日期,计算count天后的日期是什么时候,在这个函数里,还有调用Datetrue(intyear,intmonth,intday)认证输入的日期是否正确)inttimeGap(intyear_2,intmonth_2,intday_2,intyear3,intmonth3,intday3)(输入两个日期,计算它们相隔时间多少天,先验证输入的日期是否正确,调用input()函数,具体详细过程看源代码)voidinput(inty,intm,intd)(这个函数通过递归调用,因输入的日期不正确要求用户重新输入,调用了Dateture(intyear,intmonth,intday))packagedate;importjava.lang.String;importjava.util.Scanner;publicclassDatetext{publicintyear,month,day;publicintDatetrue(intyear,intmonth,intday){//验证输入的日期是否符合正常输入inty=year;intd=day;intmon=month;intg=0;//mon是月份//intnum=0;/*num=year.length();if(num!=4){System.out.println(year+年份输入不合法);}*/if(mon12||mon1){2System.out.println(month+月份输入不合法);g=-1;}elseSystem.out.println(输入月份合法);if(d31||d1)System.out.println(日期输入不合法);if(mon!=2){if(mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12){if((d31||d1)){System.out.println(日期输入不合法);g=-1;}elseSystem.out.println(输入日份合法);}else{if((d30||d1)){System.out.println(日期输入不合法);g=-1;}elseSystem.out.println(输入日份合法);}}else{if(y%4==0){if((d29||d1)){System.out.println(日期输入不合法);g=-1;}elseSystem.out.println(输入日份合法);}if(y%4!=0){if((d28||d1)){System.out.println(日期输入不合法);g=-1;}elseSystem.out.println(输入日份合法);}}returng;}publicintCountDate(intyear1,intmonth1,intday1,intcount1){//给点一个日期值,计算若干天3后的日期intyear10=year1,month10=month1,day10=day1,count=count1;System.out.println(输出+year10+年+month10+月+day10+号+到+count+天后的日期是多少?);//System.out.println(下面来计算相隔+count+后的日期);intynumber,mnumber,month2;//month2是二月份的日数if(year10%4==0){//判断年份ynumber=count/366;mnumber=count%366+day10;month2=29;}else{ynumber=count/365;mnumber=count%365+day10;month2=28;}if(ynumber=1){year10=year10+ynumber;}/*if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){day_count=31;}else{if(month!=2)day_count=30;}*/intmonth_day[]={0,31,month2,31,30,31,30,31,31,30,31,30,31};for(;mnumbermonth_day[month10];){mnumber=mnumber-month_day[month10];if(month10==12){month10=0;year10=year10+1;}month10++;}day10=mnumber;System.out.println(加上+count+天是+year10+年+month10+月+day10+号);return0;}publicinttimeGap(intyear_2,intmonth_2,intday_2,intyear3,intmonth3,intday3){4System.out.println(下面来计算两个日期相隔多少天);Stringdate1=year_2+-+month_2+-+day_2;Stringdate2=year3+-+month3+-+day3;intyear1=year_2;intmonth1=month_2;intday2=day3;intyear2=year3;intyearGap,dayGap=0;intmonth2=month3;intday1=day_2;intyearday[]={0,0};intmonth_year_day[][]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,28,31,30,31,30,31,31,30,31,30,31}};//分配月份的天数if(year1year2){yearGap=year1-year2;inttemp1;temp1=year1;year1=year2;year2=temp1;inttemp2;temp2=month1;month1=month2;month2=temp2;inttemp3;temp3=day1;day1=day2;day2=temp3;}else{yearGap=year2-year1;}intyear[]={year1,year2};intmonth[]={month1,month2};for(inti=0;i=1;i++){if(year[i]%4==0){month_year_day[i][2]=29;yearday[i]=366;}else{month_year_day[i][2]=28;yearday[i]=365;}}5for(inti=1;i(yearGap-1);i++){if((year1+i)%4==0){dayGap=dayGap+366;}else{dayGap=dayGap+365;}}if(yearGap0){//当两个日期之间有年份之差时for(;month20;){dayGap=dayGap+month_year_day[1][month2];month2--;}dayGap=dayGap+day2;for(;month[0]13;){dayGap=dayGap+month_year_day[0][month[0]];month[0]++;}dayGap=dayGap+month_year_day[0][month1]-day1;}else//当两个年份之间没有年份之差时{if(month1!=month2){//当有月份之差if(month1month2){dayGap=dayGap+month_year_day[0][month2]-day2+day1;for(;month2month1-1;){month2++;dayGap=dayGap+month_year_day[0][month2];}}elseif(month1month2){dayGap=dayGap+month_year_day[0][month1]-day1+day2;for(;month1month2-1;){month1++;dayGap=dayGap+month_year_day[0][month1];}}}elseif(month1==month2){//没有月份之差时if(day1day2){dayGap=dayGap+day1-day2;}elsedayGap=dayGap+day2-day1;}}System.out.println(日期+date1+和日期+date2+之间相隔时间为:+dayGap+天);6return0;}Datetext(inty,intm,intd){year=y;month=m;day=d;System.out.println(year+年+month+月+day+号);}publicvoidinput(inty,intm,intd){intout;out=Datetrue(y,m,d);if(out==-1){System.out.println(请重新输入);Scannersc=newScanner(System.in);inty1=sc.nextInt();intm1=sc.nextInt();intd1=sc.nextInt();y=y1;m=m1;d=d1;input(y,m,d);}else{year=y;month=m;day=d;}}publicstaticvoidmain(String[]args){System.out.println(下面来进行日期计算过程);Scannersc=newScanner(System.in);System.out.println(请输入一个日期,请输入数字日期);inty=sc.nextInt();intm=sc.nextInt();intd=sc.nextInt();Datetexttext=newDatetext(y,m,d);System.out.println(来验证一下读取的日期是否正确);text.Datetrue(y,m,d);System.out.println(请输入一个日期);inty1=sc.nextInt();intm1=sc.nextInt();intd1=sc.nextInt();text.input(y1,m1,d1);System.out.println(请输入一个相隔天数);intn=sc.nextInt();text.CountDate(text.year,text.month,text.day,n);System.out.println(下面来计算两个时间段的相隔天数);System.out.println(请输入日期:);System.out.println(请输入第一个日期:);7inty2=sc.nextInt()
本文标题:java实验2(计算日期源代码)
链接地址:https://www.777doc.com/doc-1784325 .html