|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先谈谈我对java的一些认识。我选择java,是因为他语法简单,功能强大,从web,到桌面,到嵌入式,无所不能。但当我进一步了解了java后,感叹,java原来也有许多缺点。
/**DateCalculate.java,Createdon2005-3-18*Copyright2005AAAInformationTechnologyCO.LTD.Allrightsreserved.**作成日期:2005-3-18*修正经历:**/packagecom.bjb.xyh.util;
importjava.text.DecimalFormat;//importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;
/***类形貌:本人完成的日期盘算类,能够自在扩大*@authorxieyhlamp5w@yahoo.com.cn*@versionbuild2005-3-18**/publicclassDateCalculate{/**注重格里历和儒略历交代时的日期不同*/privatestatictransientintgregorianCutoverYear=1582;/**闰年中每个月天数*/privatestaticfinalint[]DAYS_P_MONTH_LY={31,29,31,30,31,30,31,31,30,31,30,31};/**闰年中每个月天数*/privatestaticfinalint[]DAYS_P_MONTH_CY={31,28,31,30,31,30,31,31,30,31,30,31};/**代表数组里的年、月、日*/privatestaticfinalintY=0,M=1,D=2;/**介入运算用*/privateint[]ymd=null;
/***反省传进的参数是不是正当的日期*@paramdate*@throwsIllegalArgumentException*/publicstaticvoidvalidate(Stringdate)throwsIllegalArgumentException{int[]ymd=splitYMD(date);if(ymd[M]==0||ymd[M]>12){thrownewIllegalArgumentException("月份数值毛病");}if(true==isLeapYear(ymd[0])){if(ymd[D]==0||ymd[D]>DAYS_P_MONTH_LY[ymd[M]-1]){thrownewIllegalArgumentException("日期数值毛病");}}else{if(ymd[D]==0||ymd[D]>DAYS_P_MONTH_CY[ymd[M]-1]){thrownewIllegalArgumentException("日期数值毛病");}}}
/***反省传进的参数代表的年份是不是为闰年*@paramyear*@return*/publicstaticbooleanisLeapYear(intyear){returnyear>=gregorianCutoverYear?((year%4==0)&&((year%100!=0)||(year%400==0)))://Gregorian(year%4==0);//Julian}/***日期加1天,注重这里没有思索儒略历和格里历交代时相差的10天*@paramyear*@parammonth*@paramday*@return*/privateint[]addOneDay(intyear,intmonth,intday){if(isLeapYear(year)){day++;if(day>DAYS_P_MONTH_LY[month-1]){month++;if(month>12){year++;month=1;}day=1;}}else{day++;if(day>DAYS_P_MONTH_CY[month-1]){month++;if(month>12){year++;month=1;}day=1;}}int[]ymd={year,month,day};returnymd;}/***以轮回的体例盘算日期加法*@paramdate*@paramdays*@return*/publicStringaddDaysByLoop(Stringdate,intdays){validate(date);int[]ymd=splitYMD(date);for(inti=0;i<days;i++){ymd=addOneDay(ymd[Y],ymd[M],ymd[D]);}returnformatYear(ymd[Y])+formatMonthDay(ymd[M])+formatMonthDay(ymd[D]);}/***日期减1天,注重这里没有思索儒略历和格里历交代时相差的10天*@paramyear*@parammonth*@paramday*@return*/privateint[]reduceOneDay(intyear,intmonth,intday){if(isLeapYear(year)){day--;if(day<=0){month--;if(month<1){year--;month=12;}day=DAYS_P_MONTH_LY[month-1];}}else{day--;if(day<=0){month--;if(month<1){year--;month=12;}day=DAYS_P_MONTH_CY[month-1];}}int[]ymd={year,month,day};returnymd;}/***以轮回的体例盘算日期减法*@paramdate*@paramdays*@return*/publicStringreduceDaysByLoop(Stringdate,intdays){validate(date);int[]ymd=splitYMD(date);for(inti=0;i<days;i++){ymd=reduceOneDay(ymd[Y],ymd[M],ymd[D]);}returnformatYear(ymd[Y])+formatMonthDay(ymd[M])+formatMonthDay(ymd[D]);}/***指定日期加上指定的天数的操纵*@paramdate*@paramdays*@return*@throwsIllegalArgumentException*/publicStringaddDays(Datedate,intdays)throwsIllegalArgumentException{returnaddDays(formatDate(date),days);}/***指定日期加上指定的天数的操纵*@paramdate*@paramdays*@return*@throwsIllegalArgumentException*/publicStringaddDays(Stringdate,intdays)throwsIllegalArgumentException{validate(date);ymd=splitYMD(date);if(isLeapYear(ymd[Y])){ymd[D]+=days;if(ymd[D]>DAYS_P_MONTH_LY[ymd[M]-1]){ymd[M]++;ymd[D]=ymd[D]-DAYS_P_MONTH_LY[ymd[M]-1-1];if(ymd[M]>12){ymd[M]-=12;ymd[Y]++;}if(ymd[D]>DAYS_P_MONTH_LY[ymd[M]-1]){addDays(formatYear(ymd[Y])+formatMonthDay(ymd[M])+formatMonthDay(DAYS_P_MONTH_LY[ymd[M]-1]),ymd[D]-DAYS_P_MONTH_LY[ymd[M]-1]);}}}else{ymd[D]+=days;if(ymd[D]>DAYS_P_MONTH_CY[ymd[M]-1]){ymd[M]++;ymd[D]=ymd[D]-DAYS_P_MONTH_CY[ymd[M]-1-1];if(ymd[M]>12){ymd[M]-=12;ymd[Y]++;}if(ymd[D]>DAYS_P_MONTH_CY[ymd[M]-1]){addDays(formatYear(ymd[Y])+formatMonthDay(ymd[M])+formatMonthDay(DAYS_P_MONTH_CY[ymd[M]-1]),ymd[D]-DAYS_P_MONTH_CY[ymd[M]-1]);}}}returnformatYear(ymd[Y])+formatMonthDay(ymd[M])+formatMonthDay(ymd[D]);}/***指定日期减往指定的天数的操纵*@paramdate*@paramdays*@return*@throwsIllegalArgumentException*/publicStringreduceDays(Datedate,intdays)throwsIllegalArgumentException{returnreduceDays(formatDate(date),days);}/***指定日期减往指定的天数的操纵*@paramdate*@paramdays*@return*@throwsIllegalArgumentException*/publicStringreduceDays(Stringdate,intdays)throwsIllegalArgumentException{validate(date);ymd=splitYMD(date);if(isLeapYear(ymd[Y])){ymd[D]-=days;if(ymd[D]<=0){ymd[M]--;if(ymd[M]<1){ymd[M]+=12;ymd[Y]--;}ymd[D]=ymd[D]+DAYS_P_MONTH_LY[ymd[M]-1];if(ymd[D]<=0){reduceDays(formatYear(ymd[Y])+formatMonthDay(ymd[M])+formatMonthDay(1),abs(ymd[D]-1));}}}else{ymd[D]-=days;if(ymd[D]<=0){ymd[M]--;if(ymd[M]<1){ymd[M]+=12;ymd[Y]--;}ymd[D]=ymd[D]+DAYS_P_MONTH_CY[ymd[M]-1];if(ymd[D]<=0){reduceDays(formatYear(ymd[Y])+formatMonthDay(ymd[M])+formatMonthDay(1),abs(ymd[D]-1));}}}returnformatYear(ymd[Y])+formatMonthDay(ymd[M])+formatMonthDay(ymd[D]);}/***格局化一个日期字符串*@paramdate*@return*/publicstaticStringformatDate(Datedate){SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");returnsdf.format(date);}/***将代表日期的字符串支解为代表年代日的整形数组*@paramdate*@return*/publicstaticint[]splitYMD(Stringdate){int[]ymd={0,0,0};ymd[Y]=Integer.parseInt(date.substring(0,4));ymd[M]=Integer.parseInt(date.substring(4,6));ymd[D]=Integer.parseInt(date.substring(6,8));returnymd;}/***将不敷两位的月份或日期补足为两位*@paramdecimal*@return*/publicstaticStringformatMonthDay(intdecimal){DecimalFormatdf=newDecimalFormat("00");returndf.format(decimal);}/***将不敷四位的年份补足为四位*@paramdecimal*@return*/publicstaticStringformatYear(intdecimal){DecimalFormatdf=newDecimalFormat("0000");returndf.format(decimal);}/***取相对值操纵*@paramnum*@return*/publicstaticintabs(intnum){return(num>0)?num:-num;}/***测试用main函数*@paramargs*/publicstaticvoidmain(String[]args)throwsException{StringcurrDate="20001231";StringcurrDate2="20001231";intdays=-36600;Datedate=null;DateCalculatedc=newDateCalculate();SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");date=sdf.parse(currDate2);System.out.println(currDate+"+"+abs(days)+"days="+dc.addDays(currDate,abs(days)));System.out.println(currDate+"-"+abs(days)+"days="+dc.reduceDays(currDate,abs(days)));
Calendarcld=Calendar.getInstance();cld.setTime(date);cld.add(Calendar.DATE,abs(days));System.out.println(currDate2+"+"+abs(days)+"days="+sdf.format(cld.getTime()));cld=Calendar.getInstance();cld.setTime(date);cld.add(Calendar.DATE,days);System.out.println(currDate2+"-"+abs(days)+"days="+sdf.format(cld.getTime()));System.out.println(dc.addDaysByLoop(currDate2,abs(days)));System.out.println(dc.reduceDaysByLoop(currDate2,abs(days)));}}
写这些代码的念头来自比来进修SHELL剧本的感觉。
目标以下,先搁着,等偶然间再作增补:1.制止递回算法的滥用2.临界值的掌控3.日期盘算的细节
C#是盗用了Java的源代码,仿照开发的,原因是Java是开源的啊,盗了也白盗,还有一点,开发C#语言的团队是就是开发Java语言的团队,是微软重金挖过去的啊 |
|