请教,给出某年某月某日判断是今年第几天,并用函数的方法调用,
本帖最后由 u011325979 于 2013-07-06 20:00:25 编辑
问题 在 下面函数里,FOR循环的条件,for (int i = 0; i < ary.length; i++) 如何让i<小于月份,比如算3月的, 那么 就应该是 i=1;i<4或i<=3 如何做到这样,新手才学习
public class Work {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int year=2012;
int mouth=1;
int day=10;
int days=GetDaysOfOneYear(2012,2,10);
System.out.println(days);
}
static int GetDaysOfOneYear(int year,int month,int day){
int result=0;
int[] ary=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
if(year%4==0 && year%100!=0 || year%400==0){
ary[1]=29;
for (int i= 1; i <ary.length ; i++) {
result=result+ary[i-1];
}
result=result+day;
}else{
for (int i = 0; i < ary.length; i++) {
result=result+ary[i-1];
}
result=result+day;
}
return result;
}
}
------解决方案--------------------import java.util.Calendar;
import java.util.GregorianCalendar;
public class Hello {
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.MONTH, 0); // 0表示一月,1表示二月
calendar.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
}
}