日時をフォーマットする(GroovyのDateクラス版)
日時をフォーマットするで一度やったのだが、
JavaのSimpleFormatクラスで全然groovyっぽくなかった。
groovyのDateクラスのフォーマットを使えばできると
id:wandersonsantosからコメントをもらったのでやり直し。
http://groovy.codehaus.org/groovy-jdk/java/util/Date.html
ソース
new Date().format("yyyy/MM/dd(E)") new Date().format("h:mm a") new Date().format("EEE, MMM d, ''yy") new Date().format("EEE, d MMM yyyy HH:mm:ss Z") new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSSZ") new Date().format("EEE, MMM d, ''yy", Locale.US)
実行結果
groovy:000> load DayFormat.groovy ===> 2009/12/27(日) ===> 9:51 午後 ===> 日, 12 27, '09 ===> 日, 27 12 2009 21:51:20 +0900 ===> 2009-12-27T21:51:20.468+0900 ERROR groovy.lang.MissingMethodException: No signature of method: java.util.Date.format() is applicable for argument types: (java.lang.String, java.util.Locale) values: [EEE, MMM d, ''yy, en_US] at groovysh_evaluate.run (groovysh_evaluate:2) ...
↑
format(String format,Locale locale)メソッドがないので
残念ながら、ロケールの変更はできない。
なので、Locale.USを指定している最後の実行は
MissingMethodException: No signature of method:
になる。