高阶面试-hw算法整理-1154一年中的第几天

public int dayOfYear(String date) {
	// format YYYY-MM-DD
	int year = Integer.parseInt(date.substring(0, 4));
	int month = Integer.parseInt(date.substring(5, 7));
	int day = Integer.parseInt(date.substring(8));
	int[] daysOfMonthList = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	// 闰月
	if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
	daysOfMonthList[1]++;
	}
	int daysBeforeThisMonth = 0;
	if (month > 1){
	for (int i = 0; i < month - 1; i++) {
	daysBeforeThisMonth += daysOfMonthList[i];
	}
	}
	return day+daysBeforeThisMonth;
}
上一篇:KCL 测试用例框架设计 Python java go c++


下一篇:qt tabWidget 详解