光靠月份就能定位天数吗?我指定一个2月份,那他到底是28天呢还是29天呢? 加个年份吧自己写个函数: public static int getActualMaximumDays(int year,int month){ int days=0; if(month<0 || month>11){ days=-1; }else{ Calendar cal=new GregorianCalendar(year,month,1); days=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
} return days; }
class YearDisplay { public static void main(String[] arguments) { int year = 2000; if (arguments.length > 0) year = Integer.parseInt(arguments[0]); for (int month = 1; month < 13; month++) for (int day = 1; day <= countDays(month, year); day++) System.out.println(month + "/" + day + "/" + year); } static int countDays(int month, int year) { int count = -1; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: count = 31; break; case 4: case 6: case 9: case 11: count = 30; break; case 2: if (year % 4 == 0) count = 29; else count = 28; if ((year % 100 == 0) & (year % 400 != 0)) count = 28; } return count; } }
正好写了一段,有些东西是后来加的,主要看看下面的cday().package handyday;//import java.io.*;public class Day{ /* public static void main(String[] args) throws IOException{ int year=0,month=0; String iny=null,inm=null;
do{ System.out.println("Please input the right year and month"+"\n"+"input year:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); iny = br.readLine();
if (iny.length()==0) { System.out.print("aaaa"+iny); continue; }
System.out.println(year+"."+month+" has "+cday(year,month)+" days"); }*/
public Day(){}
public int cday(int y,int m){ int day=0; switch(m){ case 4 : case 6 : case 9 : case 11 : day = 30;break; case 2 : if (y%4==0){day=29;}else{day=28;} if ((y%100==0)&(y%400!=0)){day=28;}break; default : day = 31;break;
int days=0;
if(month<0 || month>11){
days=-1;
}else{
Calendar cal=new GregorianCalendar(year,month,1);
days=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
return days;
}
public static void main(String[] arguments) {
int year = 2000;
if (arguments.length > 0)
year = Integer.parseInt(arguments[0]);
for (int month = 1; month < 13; month++)
for (int day = 1; day <= countDays(month, year); day++)
System.out.println(month + "/" + day + "/" + year);
} static int countDays(int month, int year) {
int count = -1;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count = 31;
break;
case 4:
case 6:
case 9:
case 11:
count = 30;
break;
case 2:
if (year % 4 == 0)
count = 29;
else
count = 28;
if ((year % 100 == 0) & (year % 400 != 0))
count = 28;
}
return count;
}
}
/* public static void main(String[] args) throws IOException{
int year=0,month=0;
String iny=null,inm=null;
do{
System.out.println("Please input the right year and month"+"\n"+"input year:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
iny = br.readLine();
if (iny.length()==0) {
System.out.print("aaaa"+iny);
continue;
}
System.out.println("input month:");
inm=br.readLine();
if(inm == null) {System.out.print("bbb"+inm); continue;}
year=Integer.parseInt(iny);
month=Integer.parseInt(inm);
}while((month<1)||(month>12));
System.out.println(year+"."+month+" has "+cday(year,month)+" days");
}*/
public Day(){}
public int cday(int y,int m){
int day=0;
switch(m){
case 4 :
case 6 :
case 9 :
case 11 : day = 30;break;
case 2 : if (y%4==0){day=29;}else{day=28;}
if ((y%100==0)&(y%400!=0)){day=28;}break;
default : day = 31;break;
}
return(day);
}
}