用java写一个今天是今年的第几天的程序
2个回答
展开全部
public int orderDate(Date date){
int dateSum = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(date);
System.out.println(dateStr);
int year = Integer.valueOf(dateStr.substring(0,4));
int month = Integer.valueOf(dateStr.substring(5,7));
int day = Integer.valueOf(dateStr.substring(8,10));
for (int i = 1; i < month; i++){
switch(i){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:dateSum += 31; break;
case 4: case 6: case 9: case 11:dateSum += 30; break;
case 2:
if(((year % 4 == 0) & (year % 100 != 0)) | (year % 400 == 0))
dateSum += 29;
else dateSum += 28;
}
}
return dateSum = dateSum + day;
}
int dateSum = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(date);
System.out.println(dateStr);
int year = Integer.valueOf(dateStr.substring(0,4));
int month = Integer.valueOf(dateStr.substring(5,7));
int day = Integer.valueOf(dateStr.substring(8,10));
for (int i = 1; i < month; i++){
switch(i){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:dateSum += 31; break;
case 4: case 6: case 9: case 11:dateSum += 30; break;
case 2:
if(((year % 4 == 0) & (year % 100 != 0)) | (year % 400 == 0))
dateSum += 29;
else dateSum += 28;
}
}
return dateSum = dateSum + day;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询