表示数值的字符串
class Solution { public boolean isNumber(String s) { if(s.length()==0||s==null){ return false; } boolean numflag = false; boolean eflag =false; boolean dotflag = false; s =s.trim(); char []ss = s.toCharArray(); for(int i =0;i<ss.length;i++){ if(ss[i]>='0'&&ss[i]<='9'){ numflag = true; } else if(ss[i]=='.'){ if(dotflag||eflag){ return false; } dotflag =true; } else if(ss[i]=='e'||ss[i]=='E'){ if(eflag||!numflag){ return false; } eflag =true; numflag = false; } else if(ss[i]=='+'||ss[i]=='-'){ if(!(i==0||ss[i-1]=='e'||ss[i-1]=='E')){ return false; } } else{ return false; } } return numflag; } }
请你编写一个程序来计算两个日期之间隔了多少天。
日期以字符串形式给出,格式为 YYYY-MM-DD
,如示例所示。
输入:date1 = "2020-01-15", date2 = "2019-12-31" 输出:15
class Solution { boolean runyear(int num){ if((num%4==0&&num%100!=0)||(num%400==0)){ return true; } return false; } public int[] zhuan (String s){ String sc[] =s.split("-"); int a[] =new int[sc.length]; for(int i=0;i<sc.length;i++){ a[i] =Integer.parseInt(sc[i]); } return a; } public int shu(int a[]){ int year = a[0],month=a[1],day =a[2]; int ans =0; ans+=day-1; //天数 --> year-month-01 int mon[] ={0,31,28,31,30,31,30,31,31,30,31,30,31}; //月贡献的天数 while(month>0){ month--; ans+=mon[month];//0月为0天 if(month==2&&runyear(year)) ans+=1; } //-->: year-01-01 ans+=365*(year-1971); ans+=((year-1)/4)-(1971/4); ans-=((year-1)/100)-(1971/100); ans+=((year-1)/400)-(1971/400); return ans; } public int daysBetweenDates(String date1, String date2) { int s1[] =zhuan(date1); int s2[] =zhuan(date2); return Math.abs(shu(s1)-shu(s2)); } }