JavaだとCalendarとか使わないといけなくて何かと面倒
Groovyなら便利なのがあるよ!
TimeCategory
使い方はこんな感じ
use(org.codehaus.groovy.runtime.TimeCategory) {
def tm = new Date() + 1.days + 25.minute - 2.years
println tm.format("yyyy/MM/dd HH:mm")
}
簡単かつ、わかりやすいよね!
use(org.codehaus.groovy.runtime.TimeCategory) {
def tm = new Date() + 1.days + 25.minute - 2.years
println tm.format("yyyy/MM/dd HH:mm")
}
def url=new URL("http://www.yahoo.co.jp")
println url.getText()
import groovyx.net.http.*
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*
// basic認証用にbase64したID/Passを作成
def testBytes = "user:pass".getBytes("ISO-8859-1")
def encoded=testBytes.encodeBase64()
def http = new HTTPBuilder("http://hogehoge.jp/requiredBasicAuth")
http.headers=["Authorization":"Basic ${encoded}"]
try {
http.get(contentType:TEXT) { resp, reader ->
def page = new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parseText(reader.text)
def data = page.depthFirst()
data.each { println it }
}
} catch(Exception e) {
println "auth failure"
}
def page = new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parse("http://www.google.co.jp")
def data = page.depthFirst()
data.each { println it }
// 各行を処理するクロージャー
def changeLine1={ line,val ->
return line[2]
}
def changeLine2={ line,val ->
return line[1]
}
def add={line,val ->
return val.isNumber() ? val.toInteger()+1 : val
}
// CSV読み込み&変換
def convClo=["",changeLine1,changeLine2,add]
def ret=[]
def headers
new File("test.csv").splitEachLine("\t"){ line ->
if(!headers) headers = line
def lineData=[]
line.eachWithIndex { val,idx ->
lineData << (convClo[idx] ? convClo[idx].call(line,val) : val)
}
ret << lineData
}
// ソート関係
def sort="ID" // SortField
def order="ASC" // ASC | DESC
def headerIdx=0
headers.eachWithIndex{ val,idx ->
if(val==sort) headerIdx=idx
}
// 出力
println ret.remove(0).join("\t")
ret=ret.sort{it[headerIdx].isNumber() ? it[headerIdx].toInteger() : it[headerIdx]}
if(order.equalsIgnoreCase("DESC")) ret=ret.reverse()
ret.each{ println it.join("\t") }