/**
* 获取今天是一年当中第多少天
*/
fun getIndexOfDayFromYear(calendar: Calendar): Int {
val year = calendar[Calendar.YEAR]
val month = calendar[Calendar.MONTH]
val day = calendar[Calendar.DAY_OF_MONTH]
return getIndexOfDayFromYear(year, month, day)
}
fun getIndexOfDayFromYear(year: Int, month: Int, day: Int): Int {
var leapYear = false//是否是闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
leapYear = true
}
var index = day
if (month >= 0) { //1月,第一个月,直接day Index
index += 0
}
if (month >= 1) { //2月,第二个月,需要加一月天数31
index += 31
}
if (month >= 2) { //3月,第三个月,需要加er月天数
if (leapYear) {
index += 29//闰年加29天
} else {
index += 28//平年加28天
}
}
if (month >= 3) { //4月,第四个月,需要加三月天数31
index += 31
}
if (month >= 4) { //5月,第五个月,需要加四月天数30
index += 30
}
if (month >= 5) { //6月,第六个月,需要加五月天数31
index += 31
}
if (month >= 6) { //7月,第七个月,需要加六月天数30
index += 30
}
if (month >= 7) { //8月,第八个月,需要加七月天数31
index += 31
}
if (month >= 8) { //9月,第九个月,需要加八月天数31
index += 31
}
if (month >= 9) { //10月,第十个月,需要加九月天数30
index += 30
}
if (month >= 10) { //11月,第十一个月,需要加十月天数31
index += 31
}
if (month >= 11) { //12月,第十二个月,需要加十一月天数30
index += 30
}
return index
}
/**
* 获取一年中的第多少天是几月几号
*/
fun getDateFromYearIndex(index: Int, year: Int): Calendar {
var leapYear = false//是否是闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
leapYear = true
}
var month = 0
var day = 1
if (leapYear) {//闰年
if (index <= 31) { //1月,第一个月,+31
month = 0
day = index
} else if (index <= 60) {//2月,+29
month = 1
day = index - 31
} else if (index <= 91) {//3月,+31
month = 2
day = index - 60
} else if (index <= 121) {//4月,+30
month = 3
day = index - 91
} else if (index <= 152) {//5月,+31
month = 4
day = index - 121
} else if (index <= 182) {//6月,+30
month = 5
day = index - 152
} else if (index <= 213) {//7月,+31
month = 6
day = index - 182
} else if (index <= 244) {//8月,+31
month = 7
day = index - 213
} else if (index <= 274) {//9月,+30
month = 8
day = index - 244
} else if (index <= 305) {//10月,+31
month = 9
day = index - 274
} else if (index <= 335) {//11月,+30
month = 10
day = index - 305
} else if (index <= 366) {//12月,+31
month = 11
day = index - 335
} else {
getDateFromYearIndex(index - 366, year + 1)
}
} else {
if (index <= 31) { //1月,第一个月,+31
month = 0
day = index
} else if (index <= 59) {//2月,+28
month = 1
day = index - 31
} else if (index <= 90) {//3月,+31
month = 2
day = index - 59
} else if (index <= 120) {//4月,+30
month = 3
day = index - 90
} else if (index <= 151) {//5月,+31
month = 4
day = index - 120
} else if (index <= 181) {//6月,+30
month = 5
day = index - 151
} else if (index <= 212) {//7月,+31
month = 6
day = index - 181
} else if (index <= 243) {//8月,+31
month = 7
day = index - 212
} else if (index <= 273) {//9月,+30
month = 8
day = index - 243
} else if (index <= 304) {//10月,+31
month = 9
day = index - 273
} else if (index <= 334) {//11月,+30
month = 10
day = index - 304
} else if (index <= 365) {//12月,+31
month = 11
day = index - 334
} else {
getDateFromYearIndex(index - 366, year + 1)
}
}
val calendar = Calendar.getInstance()
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, month)
calendar.set(Calendar.DAY_OF_MONTH, day)
return calendar
}
大简至上!