Created
August 4, 2014 16:27
-
-
Save to4iki/bc052066b26b343fc0ca to your computer and use it in GitHub Desktop.
閏年判定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Calendar | |
object YearUtil extends App { | |
println(countLeapYear(1989, 2014)) | |
/** | |
* 閏年判定 | |
* | |
* - 西暦年が4で割り切れる年は閏年 | |
* - ただし、西暦年が100で割り切れる年は平年 | |
* - ただし、西暦年が400で割り切れる年は閏年 | |
* @param year | |
* @return | |
*/ | |
def isLeapYear(year: Int) = { | |
if (year < 4) { | |
false | |
} else { | |
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) true | |
} | |
} | |
/** | |
* こっちの方がシンプル | |
* @param year | |
* @return | |
*/ | |
def isLeapYear2(year: Int) = { | |
val cal = Calendar.getInstance() // カレンダークラスのインスタンスを取得 | |
cal.set(Calendar.YEAR, year) | |
cal.getActualMaximum(Calendar.DAY_OF_YEAR) == 366 | |
} | |
// 閏年を数え上げ | |
def countLeapYear(fromYear: Int, toYear: Int): List[Int] = { | |
(fromYear to toYear).toList.filter(isLeapYear(_) == true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment