西暦 to 和暦
Posted feedbacks - Groovy
うーん、例外ばかりの汚いコードになってしまいました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // 準備
def mappingTable = [
[range:18680908..19120730,gengo:"明治"]
,[range:19120730..19261225,gengo:"大正"]
,[range:19261225..19890108,gengo:"昭和"]
,[range:19890108..99991231,gengo:"平成"]
]
try{
// 本処理開始
def input = args[0]
if(!input) throw new IllegalArgumentException()
def columns = input.split("/")
if(columns?.size() != 3) throw new IllegalArgumentException()
def year = columns[0].toInteger()
def month = columns[1].toInteger()
def date = columns[2].toInteger()
// 日付チェック
Calendar cal = Calendar.getInstance()
cal.setLenient( false )
cal.set(year, month-1, date)
cal.getTime()
def value = columns.join("").toInteger()
def matchs = mappingTable.findAll{ value in it.range }
if( matchs.empty ){
throw new IllegalArgumentException()
}
matchs.each{
if( value in it.range ){
def wayear = year - (int)(it.range[0]/10000) + 1
println "${it.gengo}${wayear}年${month}月${date}日"
}
}
} catch (IllegalArgumentException e){
println "範囲外"
}
|


ocean
#5067()
Rating0/8=0.00
>a.py 1868/12/2
明治1年12月2日
>a.py 1926/12/24
大正15年12月24日
>a.py 2007/12/01
平成19年12月1日
>a.py 1926/12/25
大正15年12月25日 昭和1年12月25日
>a.py 1868/1/2
範囲外
>a.py 1868/100/2
範囲外
see: 和暦西暦対応表
[ reply ]