根据日期得到当天是当年的第几天,下以为Java实现的二种方法:
第一种:传入一个日期类型的数据,返回一个天数(即此日期在这一年的第几天)
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;
}
第二种:传入日期的年月日,返回天数.
public int orderDate(int year,int month,int day){
int dateSum = 0;
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;
}